On a certain tap event, I ask the user to add an image. So I provide two options:
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.
Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap(); Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)
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); }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With