Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get URI of bitmap without saving it to sdcard?

Tags:

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

like image 322
Ahmed Mousa Avatar asked Sep 26 '14 12:09

Ahmed Mousa


People also ask

How do I find the URI of a picture?

Ideally, your "upload to a server" logic can work with an InputStream . In that case, call openInputStream() on a ContentResolver to get an InputStream on the content identified by the Uri .

How do I share photos without saving?

In fact sharing an image without saving it is by using cache memory. Consequently we can share it like an uri. On the other hand we can simply store the image in cache and use the uri for sharing. Furthermore we are saving bitmap to cache and use it for sharing.


2 Answers

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  } 
like image 59
Sagar Pilkhwal Avatar answered Oct 06 '22 15:10

Sagar Pilkhwal


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()

like image 32
Maveňツ Avatar answered Oct 06 '22 15:10

Maveňツ