My duty was to make it possible for an android app to send a file to a server via SFTP protocol. Well, I had really big troubles to succeed in this. I could not find good documentation or example about it , indeed.
Then I ended up with learning a library called JSCH . I started to apply it for my application but I got really big difficulties with running it. I had many many problems. Long story short, here is a neat example of JSCH library for ANDROID, which helped me very well and I was able to upload a .txt file from my android phone to the server by SFTP. YES ! I am smiling now in the middle of the night...:)
Link :
http://timarcher.com/blog/2007/04/sending-files-via-ftp-from-your-java-applications-part-2-of-2-using-jsch-for-sftp/
Many Thanks and Regards to Timothy !
Android OS Development
Monday, September 16, 2013
Friday, September 6, 2013
How to capture an image, and display it afterwards
Given that we have a "TakePicture" button which works just like the icon (with photo camera) on our home window in the mobilephone. When we click on that "TakePicture" button, we want our android phone to turn our camera on and show around by the camera. In order to do that , we have an intent as follows ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.takephoto);
Intent intent = new Intent();
// Picture from camera
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
.
.
.
.
Ok now our intent knows that it is going to have a capture action. But before making it happen, we would like to define where we are going to store the image. For this purpose, we create a file with a dynamic name by the help of date object as follows;
Calendar date = Calendar.getInstance();
SimpleDateFormat dateformatter = new SimpleDateFormat
("dd_MM_yyyy_'at'_hh_mm_ss");
//Log.e("timeStamp2: " , ""+dateformatter.format(date.getTime()));
String loggerDate = dateformatter.format(date.getTime());
String newPicFile = "myImageFolder"+ loggerDate + ".jpg";
outPath = "/sdcard/" + newPicFile;
File outFile = new File(outPath);
mCameraFileName = outFile.toString();
outuri = Uri.fromFile(outFile);
And last but not least , inside onClick() function of our button, we put the path info into our intent , and start the action ;
intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
startActivityForResult(intent, CAMERA_REQUEST);
That's it , now my app knows that if the user clicks the takePicture button, it will automatically open a new frame where you can take a new picture. And when you take the picture, it enables to you to take a new one , ignore or confirm the capture operation as follows.
And if you would like to do further operation, onActivityResult () method is there to help you.
requestCode is the value I sent above where I called the operation, and the resultCode equals to "RESULT_OK" if the user clicks the check button. In case of such action, I can set the source of an imageView (in the following screen) as the captured image's path. By this way I can show the captured image on my screen. That's it ! Congrats.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
File imgFile = new File(outPath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
}
.
.
.
.
.
.
.
Our variables defined as such ;
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private Uri outuri ;
private String outPath ="";
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/imageFolder/";
private String selectedImagePath;
final static private int NEW_PICTURE = 1;
private String mCameraFileName;
Thursday, September 5, 2013
imageView.setImageURI Null Pointer Exception (NPE)
I was trying to change the source of an "imageView" in my design , but there was a pain in the ass that stopped me progressing. I was getting the error as shown below.
Check the line including "Caused by" statement. There was no error at all, I was totally sure of it. However there was something that I was missing --> the order of the important lines of codes.
If you check the onCreate() method of my application, you may see that it seems pretty well :) (look at that carefully !!! lol ) there is a huge mistake I made though. The problem is that , setContentView(xyz) is a pretty important line of code stating that "I will Set the activity content to an explicit view. " . But somehow ( I know why, because it was in the middle of the night me was trying to build up something with my sleepless eyes ) I was successful enough to put that marked line of code between two important lines as shown below. It troubled me as expected. It took almost an hour for me to focus on my problem and fix it (for god's sake, I was so sleepless) . Please just take that line to any place with making sure that it comes right after setContentView() method. To me, programmers should not code as drivers should not drive when they are sleepless.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
setContentView(R.layout.takephoto);
//Call capture image method
/*Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
*/
.
.
.
.
}
Regards,
SS
Check the line including "Caused by" statement. There was no error at all, I was totally sure of it. However there was something that I was missing --> the order of the important lines of codes.
If you check the onCreate() method of my application, you may see that it seems pretty well :) (look at that carefully !!! lol ) there is a huge mistake I made though. The problem is that , setContentView(xyz) is a pretty important line of code stating that "I will Set the activity content to an explicit view. " . But somehow ( I know why, because it was in the middle of the night me was trying to build up something with my sleepless eyes ) I was successful enough to put that marked line of code between two important lines as shown below. It troubled me as expected. It took almost an hour for me to focus on my problem and fix it (for god's sake, I was so sleepless) . Please just take that line to any place with making sure that it comes right after setContentView() method. To me, programmers should not code as drivers should not drive when they are sleepless.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
setContentView(R.layout.takephoto);
//Call capture image method
/*Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
*/
.
.
.
.
}
Regards,
SS
Subscribe to:
Posts (Atom)

