android - 使用意图在图库中查看图像时不支持图像
问题描述
我想从我的应用程序打开存储在图库中本地存储中的图像。但它总是显示图像不受支持。
这是代码:
File fileStr = new File (Environment.getExternalStorageDirectory() + "/Storifier/test.jpg");
Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", fileStr);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpg");
startActivity(intent);
我为文件获取的 Uri 是
content://com.purple.myapplication.provider/external_files/Storifier/test.jpg
图像存储在存储中的 Storifier 文件夹中
我是新手!
解决方案
试试这个代码
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
将此添加到您的活动结果中
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image_view.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
并将其添加到 manifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
推荐阅读
- c# - 如何从活动目录用户和用户组更新中获取 C# 应用程序的更新
- python - 从 conftest.py 调用时,浏览器没有为类下的所有测试用例初始化
- linux - 将过滤后的记录存储到相同的文件名时出错
- nlp - MFCC 是否取决于音频长度?
- python - 有什么方法可以更快地运行下面的代码?
- lambda - 如何为 PyGresSQL 库获取 python v3.8 Linux 二进制文件?
- c# - C#、Microsoft Graph:获取使用 OneDrive Business 同步的共享点库的本地路径
- angular - 如何以角度分割从url获取的字符串?
- pandas - 数据框应用集未删除重复值
- yii - 创建一个 Yii 1.1 博客应用程序,带有不喜欢的帖子选项