25

I am making an app that allows the user to share an image using android intent but how to get that URI of

a bitmap without need to saving it to sd card

I have used this code that works fine, but I don't need to save this bitmap to sd card

private 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);
    }

I need to get that URI without saving that bitmap to sd card

SepJaPro2.4
  • 704
  • 9
  • 19
Ahmed Mousa
  • 580
  • 1
  • 7
  • 22

3 Answers3

21

Try this:

protected void ShareImage(Intent intent) {
    try {
        URL url = new URL(mImageUrl);
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        intent.putExtra(Intent.EXTRA_STREAM, getImageUri(mActivity, image));
    } catch (Exception e) {
        e.printStackTrace();
    }
    startActivityForResult(Intent.createChooser(intent, 1001));
}

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

public void onActivityResult(int requestCode, int resultCode, Intent data){
    //check for result is OK and then check for your requestCode(1001) and then delete the file

}
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
2

Try this way

protected void tryToShareImage(Intent intent) {
    try {
        URL url = new URL(mImageUrl);
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        intent.putExtra(Intent.EXTRA_STREAM, getImageUri(mActivity, image));
    } catch (Exception e) {
        e.printStackTrace();
    }
    startActivity(Intent.createChooser(intent, "Share using..."));
}

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

after that You can delete files using File.delete()

Maveňツ
  • 1
  • 12
  • 50
  • 89
  • when i can delete this file i dont know when the user can choose to share if i delete the file after start intent the uri dont found – Ahmed Mousa Sep 26 '14 at 12:42
  • 1
    When file uploaded successfully then you can delete file. If you are using AsyncTask then in onPostExecute you can delete. – Derek Jul 13 '17 at 06:17
-1

Accepted answer is not working for me. Maybe I missed something from that. But, every time I get the uri of the bitmap, it is saved in local storage. I couldnt find a way to delete that (extra) saved bitmap in accepted answer. I am using the following way:

First, get the uri of the bitmap:

public static Uri getUri(Context context, Bitmap bitmap) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Title", null);
        return Uri.parse(path);
    }

Then, delete the (extra) saved bitmap:

this.getContentResolver().delete(uri, null, null);

PS: Above code for deletion is for Activity. If you want to delete inside a Fragment, use this:

getActivity().getContentResolver().delete(uri, null, null);
NullByte08
  • 884
  • 10
  • 15