java - android通过图库意图选择的相机图像未显示在imageview中
问题描述
//这是我的画廊意图
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,2);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 2) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
imageView.setImageBitmap(bm);
} catch (IOException e) {
e.printStackTrace();
}
}
Uri tempUri = getImageUri(getApplicationContext(), bm);
File finalFile = new File(getRealPathFromURI(tempUri));
} }
我正在我的 android 应用程序中实现相机和画廊意图。当我通过相机捕获图像时,图像会显示在 imageview 中,但是如果我尝试通过画廊意图显示相同的相机图像,则图像不会显示。
注意:捕获的图像未通过画廊意图显示,但其他文件夹图像显示在 imageview
解决方案
错误 #1:ACTION_PICK
不使用 MIME 类型。要么ACTION_GET_CONTENT
与 MIME 类型一起使用,要么ACTION_PICK
与集合一起使用Uri
(例如,MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)。
错误 #2:您假设MediaStore
知道如何从中获取图像Uri
。由于Uri
可能不是来自MediaStore
,这是一个不正确的假设。使用 Glide 或 Picasso 将图像加载到您的ImageView
.
错误 #3:您正在主应用程序线程上加载图像。这将在加载图像时冻结您的 UI。使用 Glide 或 Picasso 将图像加载到您ImageView
的 .
错误 #4:您似乎复制了一些“获取Uri
位图”的代码。你不需要这样做。你已经有一个Uri
图像。它是data.getData()
,并且您首先使用它来尝试加载图像。
错误 #5:您似乎复制了一些声称获得“a 的真实路径Uri
”的代码。没有可靠的方法可以做到这一点。如果您想要 aFile
包含来自图像的数据,请使用ContentResolver
andopenInputStream()
获取InputStream
由 标识的内容Uri
,然后使用它将InputStream
字节复制到FileOutputStream
您控制的某个文件的 a 中。
推荐阅读
- javascript - 如何将 useRef 道具传递给两个组件以聚焦输入( textarea ) - react.js 和 typescript
- r - Quantmod / R中的滞后函数与下一个函数
- python - 围绕特定纬度和经度构建单元。如图所示。(Python, Geopandas)
- stripe-payments - 如何在订阅上测试条带charge.failed webhook
- php - 如何通过 PHP 数组中的 `index` 键将其全部添加?
- wpf - 自定义窗口样式未显示在设计视图中
- java - 使用 Android 从 Firebase 获取数据时,在 recyclerview 中膨胀子视图
- sas - SAS输出新变量缺少全名
- spring-boot - 我应该根据哪些参数来选择 ChainedTransactionManager 和 XA 实现(例如 Atomikos)?
- python - 我的 Python 代码有什么问题?Combobox1 不会从数据库中打开 Combobox2