从内部存储检索时返回黑色图像的位图

问题描述 投票:0回答:1

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

这就是我生成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(); }
android android-imageview android-bitmap android-file android-sharing
1个回答
0
投票

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

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

这是我在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>

file_paths.xml目录下的/res/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> 

希望能帮助有同样问题的人。

© www.soinside.com 2019 - 2024. All rights reserved.