【发布时间】:2018-07-23 09:51:17
【问题描述】:
我已经按照this、this 或this 等教程学习了如何使用 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 或类似的地方?
【问题讨论】:
-
您是否提供了权限和使用功能? developer.android.com/training/camera/photobasics
-
请阅读我的问题,@hasan_shaikh 您的解决方案与我的解决方案相同,我现在只是缺少 mUri,因为重新创建了活动。
-
@pskink 和 Phillipp 不是我的问题
-
请看我的回答,你也会得到uri路径
标签: android android-camera android-fileprovider