Java 将图像保存到临时目录并打开它
我想在temp dir中保存位图,并在默认图像查看器应用程序中打开它 所以我试着:Java 将图像保存到临时目录并打开它,java,android,Java,Android,我想在temp dir中保存位图,并在默认图像查看器应用程序中打开它 所以我试着: File outputDir = this.getCacheDir(); // context being the Activity pointer File outputFile = File.createTempFile(name, ".png", outputDir); OutputStream fos2; fos2 = new FileOutputStre
File outputDir = this.getCacheDir(); // context being the Activity pointer
File outputFile = File.createTempFile(name, ".png", outputDir);
OutputStream fos2;
fos2 = new FileOutputStream(outputFile);
saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos2);
if(saved) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imageUri = Uri.fromFile(outputFile);
intent.setDataAndType(Uri.parse("file://" + imageUri), "image/*");
startActivity(intent);
}
问题是图像未打开,可能是imageUri路径有问题
谢谢。首先,Uri.fromFile()
从Android 7.0开始就被禁止了。通常情况下,如果您试图使用意图来使用它,则会导致FileUriExposedException
崩溃。显然,你决定绕过检查。但是,其他人不必尊重您的文件://
Uri
,因为这也是多年来被禁止的。请切换到FileProvider
其次,您正在使用“image/*”
作为出站MIME类型。这是你的内容,所以你的工作就是告诉收件人它是什么文件格式。而且,既然您知道它是PNG,请使用image/PNG
,而不是image/*
谢谢您的回答!哦,这就是为什么当我在API19intent.setDataAndType(Uri.parse(“file://“+imageDir”),“image/png”)代码>工作,但不是api 25。。。那么,即使我使用temp dir,我也应该使用FileProvider吗?@RGS:Yes。由于文件://
Uri
的原因,它不仅不能在安卓7.0上运行,而且除了您的应用程序之外,没有其他应用程序可以访问该文件getCacheDir()
对你的应用程序来说是私有的——它不是某个系统范围的临时目录(Android实际上没有)。