如何打开使用的Andr​​oid ImageView的应用绘制文件夹中的图片?文件、夹中、图片、Andr

2023-09-06 18:22:47 作者:一人走,不回头

我想在我的绘制文件夹通过我的手机(图库,等)。默认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"/>