我想通过手机的默认 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/