Android crashing after camera Intent

后端 未结 8 1393
半阙折子戏
半阙折子戏 2020-12-04 15:13

I have an app published and one of the fundamental features is to allow the user to take a picture, and then save that photo in a specific folder on their External Storage.

相关标签:
8条回答
  • 2020-12-04 15:40

    I faced the same issue. Apparently the fix is to make the uriSavedImage as static. Not sure if this is the best way. But this works for me.

    0 讨论(0)
  • 2020-12-04 15:41

    On the camera button click event you can try this:

    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(getTempFile(this)));
            startActivityForResult(intent, TAKE_PHOTO_CODE);
    
            declare TAKE_PHOTO_CODE globally as:
            private static final int TAKE_PHOTO_CODE = 1;
    

    Add getTempFile Function in the code which will help to save the image named myImage.png you clicked in the sdcard under the folder named as your app's package name.

    private File getTempFile(Context context) {
            final File path = new File(Environment.getExternalStorageDirectory(),
                    context.getPackageName());
            if (!path.exists()) {
                path.mkdir();
            }
            return new File(path, "myImage.png");
        }
    

    Now on OnActivityResult function add this:

    if (requestCode == TAKE_PHOTO_CODE) {
                    final File file = getTempFile(this);
                    try {
                        Uri uri = Uri.fromFile(file);
                        Bitmap captureBmp = Media.getBitmap(getContentResolver(),
                                uri);
                        image.setImageBitmap(captureBmp);
                        } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    

    in case you get Memory issues than add below code:

    @Override
        protected void onPause() {
            image.setImageURI(null);
            super.onPause();
        }
    

    I hope this will help you

    0 讨论(0)
提交回复
热议问题