android - 如何使用 android imageview 应用程序打开可绘制文件夹中的图像?

标签 android android-intent imageview

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

我的图片位置:drawable/image.png

代码:

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

Uri.parse函数需要写什么才能看到图片文件?

最佳答案

为此,您必须将 drawable 文件夹中的图像复制到 SD 卡,然后将 SD 卡中文件的路径提供给 intent。

代码如下:

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 是可绘制文件夹中图像的名称

并且不要忘记在 manifest 文件中添加访问 SD 卡的权限

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

关于android - 如何使用 android imageview 应用程序打开可绘制文件夹中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19474061/

相关文章:

android - 如何添加分享到 Instagram 快拍的选项?

java - 如何使用 getDrawingCache 捕获带有阴影的屏幕截图?

java - 这个 "date": "2014-08-20 00:00:00 -0500"? 的时间格式是什么

java - 如何自动缩放图形以使其适合 Y 轴?

android - Intent 和 putExtra 不起作用

Android通过java代码prob将wrap_content设置为ImageView

android - 在android中更改imageview的高度和宽度

android - 将 Google Translation API 导入到 Android Studio

安卓/Admob : Can I set a list of testing devices in the Layout XML for the AdView?

java - 如何正确重启 Activity ?