简体   繁体   English

使用FileProvider用相机捕获图像,如何存储uri

[英]Capture image with camera using FileProvider, how to store uri

I have followed tutorials such as this , this or this about how to use FileProvider to take a picture with the camera and store it on a temp file for later upload. 我遵循了诸如thisthisthis之类的教程,内容涉及如何使用FileProvider用相机拍摄照片并将其存储在临时文件中以供以后上传。

They all generate a file, get the uri using file provider, and then call the CAmera intent with this uri: 它们都生成一个文件,使用文件提供程序获取uri,然后使用此uri调用CAmera意图:

 val intent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
        if (uri != null) {
            try {
                if (intent.resolveActivity(activity.packageManager) != null) {
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
                    activity.startActivityForResult(intent, resultCode)
                }
            } catch (t: Throwable) {
                Timber.e(t, t.message)
            }
        }

Then on onActivityResult they get the image using: 然后在onActivityResult他们使用以下方法获取图像:

 @Override
protected void onActivityResult(int requestCode, int resultCode,
                                              Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE) {
           //don't compare the data to null, it will always come as  null because we are providing a file URI, so load with the imageFilePath we obtained before opening the cameraIntent
        Glide.with(this).load(imageFilePath).into(mImageView);   
        // If you are using Glide.
    }
}

So to quote this tutorial "load with the imageFilePaht we obtained before opening the cameraIntent". 因此引用本教程“加载在打开cameraIntent之前获得的imageFilePaht”。

For the most part it works, but when I try with the emulator for some reason my activity is destroyed when the camera app is launched (probably low memory) and my reference of the Uri is null when I come back from the camera. 在大多数情况下,它可以工作,但是当我出于某种原因尝试使用模拟器时,启动相机应用程序时,我的活动被破坏了(可能是内存不足),当我从相机回来时,我对Uri的引用为空。

Can I retrieve this Uri from the result of the activity? 我可以从活动结果中检索此Uri吗? Or do I really have to store it in sharedpreferences or similar? 还是我真的必须将其存储在sharedpreferences或类似文件中?

and my reference of the Uri is null when I come back from the camera 当我从相机回来时,我对Uri的引用为空

Save that Uri in your saved instance state Bundle of whatever activity or fragment is calling startActivity() to start the ACTION_IMAGE_CAPTURE app. Uri保存为已保存的实例状态Bundle任何活动或片段,以调用startActivity()启动ACTION_IMAGE_CAPTURE应用。 See this sample app for a complete implementation of an ACTION_IMAGE_CAPTURE request that does this. 有关执行此操作ACTION_IMAGE_CAPTURE请求的完整实现,请参ACTION_IMAGE_CAPTURE 示例应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM