Reputation: 1338
I want to let the user set something of a contact icon, the user can either take a picture or choose one from the gallery. I have the following start activity for result, noting I handle the two differently here, but they should instead be handled the same:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case Constants.TAKE_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
File file = new File(selectedImage.getPath());
mPhoto = decodeFile(file);
Bitmap croppedImage = cropImage(mPhoto);
Bitmap resized = Bitmap.createScaledBitmap(croppedImage, 100, 100, true);
Bitmap finalPhoto = getRoundedRectBitmap(resized, 100);
imageView.setImageBitmap(finalPhoto);
}
break;
case Constants.CHOOSE_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
break;
}
The second case, CHOOSE_PHOTO works. I choose a photo from the gallery, it gets placed in the ImageView. The thing is, the methods I've created in the first case, TAKE_PHOTO are kind of important, the Bitmap gets reshaped into a circle and cropped accordingly; for small sized pictures this worked exactly how I wanted it to. The main error was Out of Memory. I looked into it on SO and the found the following method to handle this:
private Bitmap decodeFile(File f) {
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
//The new size we want to scale to
final int REQUIRED_SIZE = 70;
//Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
I thought that this would take Bitmap dimensions, record things before loading Bitmap into memory, then scale the Bitmap before loading it until OOM error was avoided entirely. However, when I ran the code the app still crashed upon trying to confirm a taken photo for selection, as if an OOM error had indeed occurred. I had the following Logcat at the time of the crash:
07-25 11:01:51.396 13054-13054/com.example.android.home E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.android.home/com.tabletnanny.ChildSelection}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3322)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3365)
at android.app.ActivityThread.access$1200(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1315)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.tabletnanny.ChildSelection.onActivityResult(ChildSelection.java:126)
at android.app.Activity.dispatchActivityResult(Activity.java:5242)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3318)
I've been looking into this for the greater part of a few hours and still can't seem to figure out how I can effectively load the Bitmap. I looked into the Android Dev site, where they had a useful tutorial on handling large Bitmaps, but their solution was very similar to the one I found on SO in the decodeFile
method. What am I missing here?
Upvotes: 0
Views: 3122
Reputation: 519
You can try this for getting bitmap in ActivityForResult()
Bitmap picture = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
picture.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Upvotes: 3