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

谁说胖子不能爱 提交于 2019-12-05 21:53:28

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"/>

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.

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!