My Android camera Uri is returning a null value, but the Samsung fix is in place, help?

前端 未结 2 401
野的像风
野的像风 2020-11-30 03:11

So I am aware of the camera\'s issue on Samsung devices. You need to create a Uri before calling the camera intent like so:

Intent cameraIntent = new Intent(         


        
相关标签:
2条回答
  • 2020-11-30 03:45

    I can suggest to create the initial (temporary) file at first, then pass the Uri in the Intent. In the function "onActivityResult()" you will have the file with the filled image.

    File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "folderName");
    dir.mkdirs();
    
    if (dir.exists()) {
        try {
            imageFile = File.createTempFile("IMG_", ".jpg", dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    if (imageFile != null) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
        activity.startActivityForResult(cameraIntent, REQUEST_CODE);
    } 
    
    0 讨论(0)
  • 2020-11-30 03:49

    Your activity gets destroyed during the Camera activity operation and re-created afterwards. You should use onSaveInstanceState/onRestoreInstanceState mechanism in your activity to preserve the image URI (and any other data) upon the activity restarts.

    Like this:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mImageUri != null) {
            outState.putString("cameraImageUri", mImageUri.toString());
        }
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState.containsKey("cameraImageUri")) {
            mImageUri = Uri.parse(savedInstanceState.getString("cameraImageUri"));
        }
    }
    
    0 讨论(0)
提交回复
热议问题