Not much to it, really.
BitmapFactory.Options options = new BitmapFactory.Options();
// Get the dimensions only.
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
int width = options.outWidth;
int height = options.outHeight;
// At this point, change width and height to retain the aspect
ratio
but use a smaller size.
// What you do here depends on the size of the images you
expect.
// If you always want to use a fixed size, you can skip the step
above entirely and
// just set width and height to those values.
options.inJustDecodeBounds = false;
options.outWidth = width;
options.outHeight = height;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
drawable = new BitmapDrawable(bitmap);
For good measure, I'm also using my own temporary storage to avoid
having the BitmapFactory allocate and deallocate it twice. Do this
when filling out the options object:
options.inTempStorage = tempStorage;
And add this to your class:
byte[] tempStorage = new byte[16 * 1024];
(Obviously, this means that your function is not re-entrant, so keep
that in mind if you do any sort of multi-threaded shenanigans.)
(Likewise, if you want to cut down on memory allocations, use a
persistent BitmapFactory.Options object as well.)
-Mike
On Dec 11, 3:12 pm, fala70 <[email protected]> wrote:
> Mike, can you explain me how did you use BitmapFactory.Options params
> to resize the image ?
>
> Stefano
>
> On 12 Dic, 00:03, EboMike <[email protected]> wrote:
>
> > I tried using BitmapFactory.Options when calling
> > BitmapFactory.decodePath() on Romain's recommendation to resize the
> > 1024x768 JPG files down to 480x320 before displaying them in a
> > Gallery, but that didn't help at all - if anything, it made things
> > worse. The app still runs out after viewing just a few images.
>
> > -Mike
>
> > On Dec 11, 10:38 am, "Justin (Google Employee)" <[email protected]>
> > wrote:
>
> > > Are you scaling the image down when you display it? Look at the longer
> > > form of BitmapFactory.decodeByteArray. The BitmapFactory.Options
> > > argument has parameters for down sampling the image on load.
>
> > > Cheers,
> > > Justin
> > > Android Team @ Google
>
> > > On Dec 11, 10:10 am, fala70 <[email protected]> wrote:
>
> > > > my application open in sequence 3 activities, on last I've this
> > > > situation heap:
> > > > ID Heap Size Allocated Free Used Objs.
> > > > 1 2,945 MB 1,936 MB 1,009 MB 65,75% 38.365
>
> > > > at any case I've a problem that I can't solve againg after much time
> > > > that I am on. I need to show a list, grid or gallery of a few photo
> > > > selected from user from internal or external memory. For that I used:
>
> > > > Uri target = Media.INTERNAL_CONTENT_URI;
> > > > Intent intent = new Intent(Intent.ACTION_PICK, target);
> > > > startActivityForResult(intent, GET_IMAGE_FROM_FOLDER);
>
> > > > and that work good. Then I know the picture id and if it is in
> > > > internal or external media.. Now I need to show the thumbnail of these
> > > > picture selected but that is impossible I can't do it for too much
> > > > memory request. All picture are taked from camera.
>
> > > > is there a solution for resize the images without goi out of memory ?
>
> > > > On 11 Dic, 13:51, Romain Guy <[email protected]> wrote:
>
> > > > > It works when you load a smaller file because you simply have very
> > > > > little amount of memory left. Your application is using too much
> > > > > memory, you need to trim it down.
>
> > > > > On Thu, Dec 11, 2008 at 1:00 AM,fala70<[email protected]> wrote:
>
> > > > > > I 've a similar problem. I am going crazy from several days.... Is
> > > > > > impossible to show images from internal or external memory captured
> > > > > > from camera. Also I tried to read a file jpg of 700KB and use
> > > > > > BitmapFactory.decodeByteArray to get the bitmap, but I receive that
> > > > > > exeption:
>
> > > > > > 12-11 00:50:27.819: ERROR/dalvikvm-heap(3547): 6291456-byte external
> > > > > > allocation too large for this process.
>
> > > > > > if I try to load an image smaller (around 4K)
> > > > > > BitmapFactory.decodeByteArray work good.
>
> > > > > > somebody has a solution ?
>
> > > > > --
> > > > > Romain Guywww.curious-creature.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---