0

I am trying to share an image. To do so i need the path of the bitmap to send it in Uri. The current line i have is deprecated and depending on the API result Null.

            String path = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Challengers", null);

What is the proper way to get the path of a Bitmap to share a Bitmap to other app using Uri?

Thanks in advance for your help.

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
Chewee133
  • 55
  • 7
  • 1
    Save your `Bitmap` to your app's internal storage, and then set up a `FileProvider` to serve it up to external apps. There's a pretty complete example in [this answer](https://stackoverflow.com/a/39619468). – Mike M. Apr 07 '20 at 04:08

3 Answers3

0

Finally found what was causing problems. I had put permissions in the Manifest but also needed to add a couple lines in onCreate

    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode);
    onRequestPermissionsResult(requestCode,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},grantResults);
Chewee133
  • 55
  • 7
0

This might help

public static Bitmap getBitmapFromURL(String imgUrl) {
try {
    URL url = new URL(imgUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    Bitmap myBitmap = BitmapFactory.decodeStream(input);
    return myBitmap;
} catch (IOException e) {
    // Log exception
    return null;
}
}
-1

You can try below code to get Image Uri,

public Uri getImageUri(Context inContext, Bitmap inImage) {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}
Mob Dev
  • 854
  • 4
  • 9