繁体   English   中英

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

[英]Save image to temp dir and open it

我想在临时目录中保存 bitmap 并在默认图像查看器应用程序中打开它。

所以我尝试:

   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 路径有问题?

谢谢。

首先,从 Android 7.0 起, Uri.fromFile()实际上已被禁止。 通常,您尝试使用Intent使用它会因FileUriExposedException而崩溃。 显然,您决定尝试绕过检查。 但是,没有其他人必须尊重您的file:/// Uri ,因为这已被禁止多年。 请切换到FileProvider

其次,您使用"image/*"作为出站 MIME 类型。 这是您的内容,因此您的工作就是告诉收件人它是什么文件格式。 而且,既然您知道它是 PNG,请使用image/png ,而不是image/*

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM