suphero
suphero

Reputation: 517

How to open an image in drawable folder using android imageview applications?

I want to open an image in my drawable folder via default imageview applications of my phone (Gallery, etc.).

My image location: drawable/image.png

Code:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(""), "image/*");
startActivity(intent);

What do I need to write in Uri.parse function to see the image file?

Upvotes: 3

Views: 5257

Answers (3)

Abhishek V
Abhishek V

Reputation: 12536

To do that you have to copy your image in drawable folder to SD card and then give the path of the file in SD card to intent.

Here is the code:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.imagename);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "test.jpg");
        try {
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        Uri path = Uri.fromFile(f);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "image/*");
        startActivity(intent);

Note that imagename is the name of the image in your drawable folder

And don't forget to add permission for accessing SD card in manifest file

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

Upvotes: 4

javad
javad

Reputation: 835

you can access your image by this code :

R.drawable.image

for example you can use this code in your "onCreate" method:

ImageView img = new ImageView(this);
img.setImageDrawable(R.drawable.image);

Upvotes: 0

hardartcore
hardartcore

Reputation: 17037

I don't think you can do that. This drawable is private for your application and other apps can't use it. You should create your own ImageViewer or copy these drawables to SD card and set that Uri when starting the Intent.

Upvotes: 1

Related Questions