使用 Intent 查看库中的图像时不支持图像



我想从我的应用程序中打开存储在图库中本地存储中的图像。但它始终表明图像不受支持。

这是代码:

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

并将其添加到清单中.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

相关内容

最新更新