Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将图像保存到临时目录并打开它_Java_Android - Fatal编程技术网

Java 将图像保存到临时目录并打开它

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

我想在temp dir中保存位图,并在默认图像查看器应用程序中打开它

所以我试着:

   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/*

谢谢您的回答!哦,这就是为什么当我在API19
intent.setDataAndType(Uri.parse(“file://“+imageDir”),“image/png”)工作,但不是api 25。。。那么,即使我使用temp dir,我也应该使用FileProvider吗?@RGS:Yes。由于
文件://
Uri
的原因,它不仅不能在安卓7.0上运行,而且除了您的应用程序之外,没有其他应用程序可以访问该文件
getCacheDir()
对你的应用程序来说是私有的——它不是某个系统范围的临时目录(Android实际上没有)。