select pitcure may lead to excced memory and crash?

39 views
Skip to first unread message

huanwu

unread,
Oct 7, 2009, 11:07:21 PM10/7/09
to Android Developers
i got a problem
i intend to use

intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);

to select picture from gallery in google phone, and load the selected
photo back into my application
it works ok in the emulator
but when i punt it on real google phone (HTC hero/ HTC magic)
if i capture a photo in the gallery, and select the photo,
it sometimes crash (not always)
is it because the photo is too large or something?
are there any solution? thanks!

//--------following are part of my code-----//

private void getGalleryImage()
{
//...

selectNewPicture = false;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);

}
@Override
protected void onActivityResult(int requestCode, int
resultCode,Intent data)
{
if (resultCode == RESULT_OK)
{
uriAvator = data.getData();

ContentResolver cr = this.getContentResolver();

try
{
bmpAvator = Bitmap.createBitmap(BitmapFactory.decodeStream
(cr.openInputStream(uriAvator)));

//Do Cutting
int dX = bmpAvator.getWidth();
int dY = bmpAvator.getHeight();
int dMin = Math.min(dX, dY);
if(dX > dY)
bmpAvator = Bitmap.createBitmap(bmpAvator, (dX-dMin)>>1,
0, dMin, dMin);
else if(dX < dY)
bmpAvator = Bitmap.createBitmap(bmpAvator, 0, (dY-dMin)>>1,
dMin, dMin);

//Do Scaling
bmpAvator = Bitmap.createScaledBitmap(bmpAvator, 100, 100,
true);

//Compress into jpeg

myAvator.setImageBitmap(bmpAvator);
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}

Jarcikon

unread,
Oct 8, 2009, 2:08:41 PM10/8/09
to Android Developers
Yes, the images from the camera are 2048px by 1536px. If you convert
to a bitmap, thats 2048 x 1536 x 2 = 6291456, or 6MB. This is right
at the allotted space an Activity gets. If you can live with smaller
images, I would suggest using a sampleSize of > 1 when decoding the
image. This will result in a smaller image size, thus less memory
used. I have used the following code in the past to keep my images
under a set byte count. Alternatively, you could forget about
calculating the sample size and just set it to 2. This would use
1.5MB instead of 6MB on a standard image.


//Get dimensions of image first (takes very little time)
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inJustDecodeBounds = true;
bfo.inDither = false;
bfo.inPreferredConfig = Bitmap.Config.RGB_565;
BitmapFactory.decodeStream(cr.openInputStream(uriAvator),null, bfo);

//Calculate sample size to keep image under maxFileSize
int maxFileSize = 1572864; //in bytes
int sampleSize = 1;
long fileSize = 2 * (bfo.outWidth/sampleSize) * (bfo.outHeight/
sampleSize);
while(fileSize > maxFileSize)
{
sampleSize++;
fileSize = 2 * (bfo.outWidth/sampleSize) * (bfo.outHeight/
sampleSize);
}

//Decode image using calculated sample size
bfo.inSampleSize = sampleSize;
bfo.inJustDecodeBounds = false;
bmpAvator = BitmapFactory.decodeStream(cr.openInputStream
(uriAvator),null, bfo);


-Mike-

huanwu

unread,
Oct 11, 2009, 5:59:10 AM10/11/09
to Android Developers
it works perfect!
thank you very much!
Reply all
Reply to author
Forward
0 new messages