select picture from gallery back to my app lead to crash?

All your problems with Audio, Video and Images.

select picture from gallery back to my app lead to crash?

Postby huanwu » Thu Oct 08, 2009 4:39 am

i got a problem
i use
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
to select picture from gallery and load back to my app
it works ok in the emulator
but when i put my app on real phone(HTC HERO/HTC MAGIC)
it sometimes crash when it loaded back (not always)
sometimes i capture photo first, and select it
is it because the photo size is too large or something?
does anyone know why?

//===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);
}
huanwu
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 08, 2009 4:18 am

Top

Postby chimaera » Wed Nov 04, 2009 1:38 am

Bitmap.createXXX methods can throw OutofMemory exceptions if the image is too big.

If you want to do it this way, you should use BitmapFactory with a BitmapFactory.Options.inJustDecodeBounds to compute the image bounds without actually loading the pixels.

You should also call Bitmap.recycle() to dispose of unneeded bitmaps because bitmap data is not handled by gc in the same way as other kinds of data. So after a few tries you will run out of memory.

But I believe the InputStream is already a jpeg file. You just have to dump it into a file.
chimaera
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jun 19, 2009 1:42 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 14 guests