0

I made an android app that contains : (1) A button 'capture photo' which when clicked opens the device camera. (2) An ImageView to show the full sized image captured from the camera.

What I did was save the full sized image and then load the same onto the ImageView. This works as intended in an android device running version 5.1.1. However, in a device with android version 4.1.1 and another device with version 7.0, it crashes when the 'CAPTURE PHOTO' button is pressed. I couldn't find the reason. Here's my java code for the same and thanks in advance :

public class MainActivity extends AppCompatActivity {

private ImageView  imageHolder;
private final int requestCode = 20;
private File mTempCameraPhotoFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imageHolder = (ImageView)findViewById(R.id.captured_photo);
    Button capturedImageButton = (Button)findViewById(R.id.photo_button);
    capturedImageButton.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent takePictureIntent = new 
  Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                File exportDir = new 
  File(Environment.getExternalStorageDirectory(), "TempFolder");
                if (!exportDir.exists()) {
                    exportDir.mkdirs();
                } else {
                    exportDir.delete();
                }
                mTempCameraPhotoFile = new File(exportDir, "/" + 
 UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");
               // Log.d(LOG_TAG, "/" + 
 UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
 Uri.fromFile(mTempCameraPhotoFile));
                startActivityForResult(takePictureIntent, requestCode);
            }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(this.requestCode == requestCode && resultCode == RESULT_OK){

        String filePath = mTempCameraPhotoFile.getPath();
        Drawable img = Drawable.createFromPath(filePath);
        imageHolder.setImageDrawable(img);
        Toast.makeText(this, "Image path"+filePath, 
Toast.LENGTH_LONG).show();


    }
}

}

Sidharth Ramesh
  • 646
  • 2
  • 6
  • 21
  • 1
    Use LogCat` to examine the Java stack trace associated with your crash. One problem is that you are not holding onto `mTempCameraPhotoFile` in the saved instanced state `Bundle`, and so if your process is terminated while the camera app is in the foreground, `mTempCameraPhotoFile` will be `null` in `onActivityResult()`. – CommonsWare Aug 12 '17 at 13:29
  • Check this post https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this/23353174#23353174 for more info about obtaining a stack trace. – Josef Adamcik Aug 12 '17 at 13:42
  • Thankyou. I'm just a beginner so pardon me if I'm wrong. but the way I understood that onActivityResult() would be invoked only after the second activity(in this case the camera activity) has finished. So the case you mentioned where my camera activity is in foreground never seems to be happening 'cause the app itself crashes as soon as 'CAPTURE_Photo' button is pressed.isn't it? – Sidharth Ramesh Aug 12 '17 at 13:42
  • You are not checking the return value of mkdirs(). Further you will sometimes delete that dir, also without checking return value, and still use that dir in following code. – greenapps Aug 12 '17 at 14:32

0 Answers0