【问题标题】:Capture image with camera using FileProvider, how to store uri使用 FileProvider 使用相机捕获图像,如何存储 uri
【发布时间】:2018-07-23 09:51:17
【问题描述】:

我已经按照thisthisthis 等教程学习了如何使用 FileProvider 使用相机拍照并将其存储在临时文件中以供以后上传。

它们都生成一个文件,使用文件提供程序获取 uri,然后使用此 uri 调用 CAmera Intent:

 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)
            }
        }

然后在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.
    }
}

所以引用本教程“加载我们在打开cameraIntent之前获得的imageFilePaht”。

在大多数情况下它可以工作,但是当我出于某种原因尝试使用模拟器时,我的活动在相机应用程序启动时被破坏(可能内存不足)并且当我从相机返回时我的 Uri 引用为空.

我可以从活动结果中检索此 Uri 吗?还是我真的必须将其存储在 sharedpreferences 或类似的地方?

【问题讨论】:

标签: android android-camera android-fileprovider


【解决方案1】:

当我从相机回来时,我对 Uri 的引用为空

Uri 保存在您保存的实例状态Bundle 中,无论活动或片段调用startActivity() 以启动ACTION_IMAGE_CAPTURE 应用程序。有关执行此操作的 ACTION_IMAGE_CAPTURE 请求的完整实现,请参阅 this sample app

【讨论】:

    猜你喜欢
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多