0

I'm having an ImageView in which I'm having a bitmap. I'm storing it in Internal Storage in the form of .png and then want to share using shareIntent. But when trying to share, it is retrieving me a black image rather than a QR code which was my original image.

This is how I'm generating QR Code and displaying it in 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(); }

In share button:

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(); }
itsmysterybox
  • 2,748
  • 3
  • 21
  • 26

1 Answers1

0

This is an old post, however, I ran into a similar problem and thought I could share the solution here which might help the other developers having the same problem.

The problem in my case was an incorrect file path. I was getting a black screen after selecting an image from the internal storage as well. I fixed the file path and the problem went away.

Here is the file provider that I added in the 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>

And the file_paths.xml file under the /res/xml/ directory was fixed like the following.

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

Hope that helps someone having the same problem.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98