When i try to get the Uri from a picture taken with the camera i get null

后端 未结 2 692
梦毁少年i
梦毁少年i 2020-12-12 00:18

here is my problem, when i try to get the Uri from the picture that I take whit the camera i get null with the camera on emulator and my device, but only whit the system cam

相关标签:
2条回答
  • 2020-12-12 00:26

    I think the issue has to do with your managing of the outputFileUri member var across the standard Android activity lifecycle.

    When you call StartActivityForResult, your activity is getting stopped and probably destroyed. When capture intent returns, your activity gets re-created and resumed. You probably need to be persisting the value of outputFileUri across those lifecycle events.

    See onCreate and onSaveInstanceState.

    The only explination I have as to why the native app does this while whatever 3rd party camera app doesnt would be the somehow the native one consumes resources to the extent that your activity does actually get destroyed, wiping out your outputFileUri.

    It might also be worth looking at the resultcode returned in onActivityResult to make sure camera intent says OK in addition to checking the uri/filepath to see that an image actually got captured. I bet it did.

    0 讨论(0)
  • 2020-12-12 00:45

    Android might have killed off (and restarted) your activity before you get into onActivityResult, for instance because you rotated your device while taking the picture. Try to store and restore outputFileUri with the rest of the Activity state...

    protected void onSaveInstanceState(Bundle outState)
    {
        outState.putParcelable("outputFileUri", outputFileUri);
    }
    

    ...

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        if (savedInstanceState != null)
        {
            outputFileUri= savedInstanceState.getParcelable("outputFileUri");
        }
    }
    
    0 讨论(0)
提交回复
热议问题