Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Uri object from Bitmap

On a certain tap event, I ask the user to add an image. So I provide two options:

  1. To add from gallery.
  2. To click a new image from camera.

My aim is to keep a list of "uri"s related to those images.

If the user chooses gallery, then I get the image uri (which is quite simple). But if he chooses camera, then after taking a picture, I am getting the Bitmap object of that picture.

Now how do I convert that Bitmap object to uri, or in other words, how can I get the relative Uri object of that bitmap object?

Thanks.

like image 919
Darshan Bidkar Avatar asked Sep 23 '12 19:09

Darshan Bidkar


People also ask

How do I get bitmap from imageView?

Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap(); Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)


2 Answers

I have same problem in my project, so i follow the simple method (click here) to get Uri from bitmap.

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); }  
like image 69
Ajay Avatar answered Sep 20 '22 03:09

Ajay


Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); 

the line mentioned above create a thumbnail using bitmap, that may consume some extra space in your android device.

This method may help you to get the Uri from bitmap without consuming some extra memory.

public Uri bitmapToUriConverter(Bitmap mBitmap) {    Uri uri = null;    try {     final BitmapFactory.Options options = new BitmapFactory.Options();     // Calculate inSampleSize     options.inSampleSize = calculateInSampleSize(options, 100, 100);      // Decode bitmap with inSampleSize set     options.inJustDecodeBounds = false;     Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmap, 200, 200,             true);     File file = new File(getActivity().getFilesDir(), "Image"             + new Random().nextInt() + ".jpeg");     FileOutputStream out = getActivity().openFileOutput(file.getName(),             Context.MODE_WORLD_READABLE);     newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);     out.flush();     out.close();     //get absolute path     String realPath = file.getAbsolutePath();     File f = new File(realPath);     uri = Uri.fromFile(f);    } catch (Exception e) {     Log.e("Your Error Message", e.getMessage());   } return uri; }   public static int calculateInSampleSize(         BitmapFactory.Options options, int reqWidth, int reqHeight) {     // Raw height and width of image     final int height = options.outHeight;     final int width = options.outWidth;     int inSampleSize = 1;      if (height > reqHeight || width > reqWidth) {          final int halfHeight = height / 2;         final int halfWidth = width / 2;          // Calculate the largest inSampleSize value that is a power of 2 and keeps both         // height and width larger than the requested height and width.         while ((halfHeight / inSampleSize) >= reqHeight                 && (halfWidth / inSampleSize) >= reqWidth) {             inSampleSize *= 2;         }     }      return inSampleSize; } 

for more details goto Loading Large Bitmaps Efficiently

like image 22
Hanan Avatar answered Sep 24 '22 03:09

Hanan