如何打开使用的Andr​​oid ImageView的应用绘制文件夹中的图片? [英] How to open an image in drawable folder using android imageview applications?

查看:101
本文介绍了如何打开使用的Andr​​oid ImageView的应用绘制文件夹中的图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的绘制文件夹通过我的手机(图库,等)。默认ImageView的应用程序中打开图像

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

我的形象定位:可绘制/ image.png

My image location: drawable/image.png

code:

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

什么我需要在Uri.parse功能写入看到的图像文件?

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

推荐答案

要做到这一点,你必须在绘制文件夹复制图像到SD卡上,然后给在SD卡意向的文件的路径。

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.

下面是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);

注意的 imagename 的是在你的绘制文件夹中的图片

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

不要忘了添加的许可的用于访问SD卡的清单的文件

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

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

这篇关于如何打开使用的Andr​​oid ImageView的应用绘制文件夹中的图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆