'How to use from my app another app to visualize an image?
I want to program an app. In this app, the user selects an image file path and the app opens another app (Google Photos) to show that image. This image is in the public folder Downloads.
I am using Android 9.0.
What I tried:
File imageFile = new File("/storage/emulated/0/DCIM/Camera/IMG_20220203_114716.jpg");
Uri uri = Uri.fromFile(imageFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
And in AndroidManifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
I get a FileUriExposedException
.
Solution 1:[1]
SOLVED:
Uri collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
String[] projection = new String[]{MediaStore.Files.FileColumns._ID};
String selection = MediaStore.Files.FileColumns.DATA + "=?";
String[] selectionArgs = new String[]{"/storage/emulated/0/DCIM/Camera/IMG_20220203_114716.jpg"};
String sortOrder = null;
Cursor cursor = getApplicationContext().getContentResolver().query(collection, projection, selection, selectionArgs, sortOrder);
cursor.moveToFirst();
Uri uri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL, cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID)));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setPackage("com.google.android.apps.photos");
intent.setDataAndType(uri, "image/*");
startActivity(intent);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |