-2

I need to open a photo, and I have red that I can use the default android gallery, but I can't get it work.
I have looked at severals forums, and finally I get this code, but it opens a black image.
I'm using api minimum 16 and target 29, testing on api 29.
Someone can help me?
Thanks.

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("content://"+"/storage/emulated/0/photo.jpg"), "image/*");
startActivity(intent);

Already tried Uri.fromFile() but doesn't work.

2 Answers2

2

You can use Uri.fromFile() instead

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/storage/emulated/0/photo.jpg")), "image/*");
startActivity(intent);

but you will get FileURIExposed error so you need to add this code in your oncreate()

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build);

Note: don't use hardcoded path use Environment

Not the best answer but its work well

Jerson
  • 1,700
  • 2
  • 10
  • 14
  • No this will not work as it will throw a FileUriExposedException when strict mode not used. And of course you will not use a strict mode hack but a FileProvider. – blackapps Oct 01 '20 at 16:28
  • i tried that on but it seems work, but not recommend to disable strictmode, although it can be achieve using file provider as you said – Jerson Oct 01 '20 at 16:32
  • You should use a FileProvider and not that hack. That's it. – blackapps Oct 01 '20 at 16:34
  • @Jerson I don't use hardcoded path in my app but I used it in this post to be more readable for you – mistermaster Oct 01 '20 at 17:59
  • @Jerson your second code gives me: cannot resolve symbol "build", can you provide an example about FileProvider? – mistermaster Oct 01 '20 at 18:00
0

This is the final working code, thanks to all to let me know the FileProvider

AndroidManifest.xml

<manifest>
    ...
    <application>
        ...
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.myproject.myapp.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>

res/xml/filepaths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="/" name="myInternalMemory" />
</paths>

MainActivity.java

Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.myproject.myapp.fileprovider", new File(filenamePath));
context.grantUriPermission("com.myproject.myapp.fileprovider", contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(contentUri);
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
//support from Android 4.1 (API level 16) to Android 5.1 (API level 22) inclusive
intent.setClipData(ClipData.newRawUri("", contentUri));
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);