1

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?

Community
  • 1
  • 1
Michael Haren
  • 105,752
  • 40
  • 168
  • 205

1 Answers1

1

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

Community
  • 1
  • 1
Samuel
  • 9,883
  • 5
  • 45
  • 57
  • That makes sense, thanks! If I copy them to the SD card, what then is the recommended call to launch them (i.e. the full `startactivity` call)? – Michael Haren Aug 29 '11 at 04:42
  • What's the typical way an app loads resources into its `.cache` folder? Does this need to be coded up or do I put my files in a special location and they get dropped there on install? – Michael Haren Aug 29 '11 at 04:54
  • the /sdcard/com.my.app path is created in newer version of android but in 2.1 and 2.2 i guess we have to create it. and the drawable we have to copy manually – Samuel Aug 29 '11 at 04:57