我想通过手机的默认imageview应用程序(图库等)打开可绘制文件夹中的图像。
我的图片位置:drawable / image.png
码:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(""), "image/*");
startActivity(intent);
我需要用Uri.parse函数编写什么才能查看图像文件?
为此,您必须将可绘制文件夹中的图像复制到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是可绘制文件夹中图像的名称
并且不要忘记在清单文件中添加访问SD卡的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句