【问题标题】:Bitmap returning black image while retrieving from Internal Storage从内部存储中检索时位图返回黑色图像
【发布时间】:2018-10-28 06:08:42
【问题描述】:

我有一个ImageView,其中我有一个bitmap。我以 .png 的形式将其存储在 Internal Storage 中,然后想使用shareIntent 共享。但是在尝试分享时,它正在检索我的黑色图像,而不是我原始图像的 QR 码。

这就是我生成二维码并将其显示在ImageView 中的方式:

try {
    BitMatrix bitMatrix = multiFormatWriter.encode(text,BarcodeFormat.QR_CODE,500,500);
    BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
    Bitmap bitmap_img = barcodeEncoder.createBitmap(bitMatrix);
    iv.setBackgroundColor(Color.WHITE); // iv is the ImageView
    iv.setImageBitmap(bitmap_img);

    bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap();

} catch (Exception r) { r.printStackTrace(); }

在分享按钮中:

try {
    File imagesFolder = new File(getCacheDir(), "images");

    File file = new File(imagesFolder, "shared_image.png");
    FileOutputStream fOut = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    iv.setImageBitmap(bitmap); // iv is the ImageView
    fOut.flush();
    fOut.close();
    savedImageURI = Uri.parse(file.getPath());

    file.setReadOnly(); //Readable(true, false);
    final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(Intent.EXTRA_STREAM, savedImageURI);
    intent.setType("image/png");
    startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) { e.printStackTrace(); }

【问题讨论】:

  • 请参考这个link

标签: android android-imageview android-bitmap android-file android-sharing


【解决方案1】:

这是一篇旧帖子,但是,我遇到了类似的问题,并认为我可以在这里分享解决方案,这可能有助于其他遇到相同问题的开发人员。

我的问题是文件路径不正确。从内部存储中选择图像后,我也遇到了黑屏。我修复了文件路径,问题就消失了。

这是我在AndroidManifest.xml 中添加的文件提供程序。

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.your.application.package.goes.here"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

/res/xml/ 目录下的file_paths.xml 文件修复如下。

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="documents"
        path="Android/data/com.your.application.package.goes.here/files/Pictures" />
</paths> 

希望对遇到同样问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2019-10-29
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    相关资源
    最近更新 更多