In my app, I am trying to take a picture, save it, and add it to an ImageView with this code I got from the Android Developers tutorial
private static final int REQUEST_IMAGE_CAPTURE = 1;
private String lastImagePath;
//Create temporary image file using getExtealFilesDir()
private File createImageFile() throws IOException{
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timestamp + "_";
File storageDir = getActivity().getExtealFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
lastImagePath = image.getAbsolutePath();
retu image;
}
//Start picture intent and save to file
private void takePicture(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
if(takePictureIntent.resolveActivity(getActivity().getPackageManager())!= null){
try{
photoFile = createImageFile();
}catch (IOException e){
Log.e(TAG, "Error Creating image file");
e.printStackTrace();
}
if (photoFile !=null){
Log.e(TAG, photoFile.getAbsolutePath());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK){
//Ensure the path has been set
if(lastImagePath !=null){
Log.e(TAG, "LastImagePath: " + lastImagePath);
//DEBUG:see if it actually created the file
File file = new File(lastImagePath);
if(file.exists()){
Log.e(TAG, "File exists");
}
//Decode file
Bitmap image = BitmapFactory.decodeFile(lastImagePath);
//DEBUG: see if image is null
if(image == null){
Log.e(TAG, "Image is null");
}else{
Log.e(TAG, "Image is not null");
}
//convert full sized image to thumbnail and add it to imageView
Bitmap thumbnail = ThumbnailUtils.extractThumbnail(image, 200, 200);
image1.setImageBitmap(thumbnail);
}else{
Log.e(TAG, "lastImagePath is null");
}
}
}
Sometimes this code works as expected and takes an image, saves it, loads it back into the program and adds it to the image view. But other times, it doesn't work at all and seems to fail to take the image.
Here is the logcat output for a successful image capture where the image appears in the ImageView
05-04 17:27:49.218 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172748_-1847663626.jpg
05-04 17:28:03.886 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: LastImagePath: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172748_-1847663626.jpg
05-04 17:28:04.359 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: File exists
05-04 17:28:04.825 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: Image is not null
And here is the logcat output for a failed image capture where there is no image in the ImageView
05-04 17:29:00.070 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172900_-1359930406.jpg
05-04 17:29:06.581 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: LastImagePath: /storage/emulated/0/Android/data/com.noah.stickynotes_clientside/files/Pictures/JPEG_20160504_172900_-1359930406.jpg
05-04 17:29:06.583 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: File exists
05-04 17:29:06.583 4964-4964/com.noah.stickynotes_clientside E/SUBMIT_FRAGMENT: Image is null
This seems to work randomly and fails 80% of the time. I'm using getExtealFilesDir because I want the photos to be private to the application. I also added the WRITE_EXTERNAL_STORAGE permission to my Manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I also understand that I could use the thumbnail from the image but I want to have the full sized image because I will be adding it to a class later on.
Anyone have any ideas on whats going wrong?
