I set a hard memory limit for the maximum viewing size of an image in
my gallery:
MAX_IMAGE_SIZE_IN_BYTES = Available memory divided by 2.
This will yield 8MByte (1st gen phones) or 12MByte (2nd gen phones).
This leaves me just enough memory for the rest of my app's objects.

First i try with sampleSize = 1;
When i need to load an image full-size, i need to know how many bytes
per pixels are available:
  MAX_IMAGE_SIZE_IN_BYTES / (width * height * sampleSize^2)
  If this is <=1, then i need the double the sampleSize:
                  Try the above formula again with sampleSize*2
  If this is 2 or 3, then Bitmap.Config is RGB_565.
  If this is 4 or higher, then Bitmap.Config is ARGB_8888.
  The end-result is the fine Bitmap.Config value and a sampleSize
value.

I save my image's modifications in a seperate file.

When i want to create a new image, based on the original image and the
image's modifications, then i startup a Service in a *different
process*. This process is just for saving. This means I have quite a
bit more of memory available for doing this:
MAX_IMAGE_SIZE_IN_BYTES = Available memory - 6MByte.
This will yield 10MByte (1st gen phones) or 18MByte (2nd gen phones).

Still, I can't load 2 images of 100% size, if they're large. There is
room for only one and a little extra to spare. That's why the saving
process chops up the image in chunks (Bitmap.getPixels), processes
each chunk according the the image's modification, append the modified
chunk to a file on the SD-card. Then when that's all done I recycle
the original large bitmap (frees up memory), create a new mutable
bitmap with the same dimensions of the old one and I re-assemple the
image again, in chunks (Bitmap.setPixels) from the file on the SD-
card. It is a slow process. But it works. Then I compress the new
mutable bitmap into a JPG/PNG file, insert it into the MediaStore and
create an appropriate thumbnail. Also, it takes care of any possible
rotation and handling of the EXIF (if so configured).

All this is quite a bit of work, to put it mildly. :)


On Apr 14, 1:30 pm, shaun <[email protected]> wrote:
> @Streets Of Boston, Is there anything further you can explain about
> your implementation?  Even a high-level strategy would be helpful.
>
> On Apr 14, 10:45 am, mike <[email protected]> wrote:
>
>
>
> > On 04/14/2010 07:17 AM, Streets Of Boston wrote:
>
> > > "AFAIK, there's no way to work on the full image directly"
> > > Yes there is. My application Snap FX does that. It takes quite a bit
> > > of work, but i managed to be able to apply color-effect to 5MPixel
> > > (1st gen Android phones) or 9MPixel (2nd gen Android phones) images.
>
> > I knew that I should have qualified that :) I believe the
> > "quite a bit of work" part though.
>
> > In any case, if all you're doing is a "save-for-web" kind of
> > application, downsampling is the easiest way around this problem.
>
> > Mike
>
> > > On Apr 14, 9:24 am, mike<[email protected]>  wrote:
>
> > >> On 04/14/2010 06:16 AM, Tim wrote:
>
> > >>> On Apr 14, 1:47 pm, Kamal Hasan<[email protected]>    wrote:
>
> > >>>> Hi,
> > >>>>       When I am trying to load even a image of 4 MB the application is
> > >>>> giving OOM error.Is there any alternative way to create bitmap from
> > >>>> large images? What is the maximum heap for an application?
>
> > >>>>                           FileInputStream fin = new FileInputStream(new
> > >>>> File("sdcard/DSC00712.jpg"));
>
> > >>> If the JPEG is 4 MB then the decoded image will be huge. What are the
> > >>> dimensions of the image?
>
> > >> What I've had to do in these situations is use BitmapFactory.Options ()
> > >> inSampleSize to scale the image down while it's reading the file.
> > >> AFAIK, there's no way to work on the full image directly. Sort of a
> > >> surprising result since the phone can take those pictures but can't
> > >> manipulate them directly, but they are enormous.
>
> > >> Mike- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

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

To unsubscribe, reply using "remove me" as the subject.

Reply via email to