Michael Haren
Michael Haren

Reputation: 108366

How do I load an image in the gallery app given an `R.drawable.foo` reference?

If I have this in hand:

R.drawable.foo

Which is really just a reference to a jpg file, how do I open it in Android's gallery app?

I have found a bunch of references which suggest something along the lines of this:

startActivity(
    new Intent(
        Intent.ACTION_VIEW,  
        /* what goes here for the URI?? */
    )
); 

But what URI do I use?

I'm doing this tutorial and want to open the images in the native Android app when tapped so I can get all that zooming, sharing, etc. for free.

If I can't use a URI here (as suggested here), what should I do to load the image?

Upvotes: 1

Views: 527

Answers (1)

Samuel
Samuel

Reputation: 9993

by design other applications cannot access your resources. its the same the otherway.

so you have two options.

Easyway:

copy your resource (manualy) to the sdcard and then sent the uri

this is how your URI might look

Uri.parse(Environment.getExternalStorageDirectory() + "/android/com.my.app/.cache/foo.jpg");

Environment.getExternalStorageDirectory() abstracts hard coding or mount the .cache will be a hidden folder and if you don't want your temp image to show up in gallery images then you need to create a .nomedia empty file in the .cache folder

you might need to add the sdcard permission

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

the Hardway:

your need to implement a FileContentProvider using a ContentProvider

supply you custom URI and supply your file from your FileContentProvider.Open, you may need to implement mime and other methods aswell for this

UPDATE you may also need to register your content provider in the manifest. Android - open pdf in external application

Upvotes: 1

Related Questions