开发者

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 g开发者_Python百科oes 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜