Reputation: 165
I am implementing one application in which I am attaching images to issues. These attachments are of two types,
I getting thee Images as follows
//Camera Request
Intent captureImage = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureImage.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
startActivityForResult(captureImage, CAMERA_PIC_REQUEST);
// Gallery Pic request
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_PIC_REQUEST);
The result of above is URI. And I am saving this uri.getPath() in my database.
Now problem is when I want to show these images I am fetching it with the help of uri I am having. But I am getting following exception after loading first image java.lang.OutOfMemoryError: bitmap size exceeds VM budget
I read some blogs and I came to know that memory is insufficient for loading it.
Is anybody having working solution on compressing images while showing them in a list. And recycle memory used after work is done.
Upvotes: 1
Views: 545
Reputation: 2403
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int bitmap_width = photoBitmap.getWidth();
int bitmap_height = photoBitmap.getHeight();
// where photoBitmap is your Bitmap image
if(bitmap_width >width)
bitmap_width = width ;
if(bitmap_height>height)
bitmap_height = height ;
// compressing bitmap goes here
photoBitmap = Bitmap.createScaledBitmap (photoBitmap, width, height, false);
Upvotes: 0
Reputation: 16570
You can use the following:
BitmapFactory.Options options;
options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.inTempStorage = new byte[16 * 1024];
Bitmap bm = BitmapFactory.decodeFile( pathToFile, options );
This will load only one fourth of the pixels in the image. Change the inSampleSize
value to load more/less.
Upvotes: 1