Tina Tina - 7 months ago 25
Android Question

Adding Image in Android cause: Unable to resume activity : Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null}

Please help me out with this Android camera.

The error I get is below:

Unable to resume activity {..,CreatePropertyActivity5}: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity..: java.lang.NullPointer


It also gives this warning which is strange since I compress the images:

W/OpenGLRenderer(10242): Bitmap too large to be uploaded into a texture (3264x2448, max=2048x2048)


My app allows the user to select a photo from gallery or take one using camera.
As u can figure out from the code there is no limit to the number of photos the user adds.

In the main page (createPropertyActivity*5*) the user touches the Take photo Button, she chooses the camera or gallery, then she selects (or takes) a picture, then the app goes to a page (CreatePropertyActivity*51* ) in which she can either cancel or write a description for the image.
When she touches "send photo button" she goes back to main page, the photo is sent to the server, and an image view is added to the bottom of the previously added images in the main page so the user can see all the images she has selected.

SOMETIMES (and I don't know when that happens, sometimes it's when the 4th picture is added, sometimes when the first!) It gives the error! (esp when the camera is on high resolution).

I compress the photos and everything. What am I doing wrong?
Does it have anything to do with the file name? Compression?

What is wrong in my code? I have no clue!

Here my code :

public class CreatePropertyActivity5 extends ActionBarActivity {
protected static final int SELECT_PICTURE = 1;
private static final int ACTIVITY_REQUEST_CODE_IMAGE = 100;
private static final int IMAGE_DESCRIPTION = 200;
LinearLayout ll;
private List<File> cameraImageFiles;
private JSONRequestForCreatePropertyListing propertyListing;
ImageView imageView;
Bitmap selectedImageBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_property_5);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
propertyListing = (JSONRequestForCreatePropertyListing) getIntent().getSerializableExtra("JSONRequestForCreatePropertyListing");
CreatePropertListingAsync cplp = new CreatePropertListingAsync(this, propertyListing);
cplp.execute();

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.create_property_activity5, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public void onClickTakePicture(View v) throws IOException {
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);


cameraImageFiles = new ArrayList<File>();

int i=0;
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.MEDIA_IGNORE_FILENAME, ".nomedia");

//** below 4 lines put the uri of the camera taken picture to the EXTRA_OUTPUT
File cameraImageOutputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myFileName");
cameraImageFiles.add(cameraImageOutputFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraImageFiles.get(i)));
i++;

cameraIntents.add(intent);
}

// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "add new");

// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
startActivityForResult(chooserIntent, ACTIVITY_REQUEST_CODE_IMAGE);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{
ll = (LinearLayout) findViewById(R.id.llCreatePropertyImages);

switch(requestCode) {

// For sending photos to server. We come here from activity51
case IMAGE_DESCRIPTION:
if(resultCode == RESULT_OK){
//add the image to the activity5 page.
imageView = new ImageView(this);
imageView.setPadding(0, 10, 0, 0);
ll.setVisibility(View.VISIBLE);
imageView.setImageBitmap(selectedImageBitmap);
ll.addView(imageView);




String s = imageReturnedIntent.getStringExtra("key");
//user entered description is in "key"
imageView.setTag(s);
Bitmap bitmap1 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
String img_str = Base64.encodeToString(image, 0);

//This part sends the picture to the server
ArrayList<Photos> photos = new ArrayList<Photos>();
photos.add(new Photos(new Ax(img_str)));

int id = Integer.parseInt((String) ((TextView) findViewById(R.id.txt_property_listing_ID)).getText());
int editPass = Integer.parseInt((String) ((TextView) findViewById(R.id.txt_property_listing_password)).getText());
JSONRequestForAddPhoto jr = new JSONRequestForAddPhoto(id, editPass, photos);

new AddPhotoAsync(this, jr).execute();




}
break;
//For choosing photos from gallery or taking one with camera
case ACTIVITY_REQUEST_CODE_IMAGE:
if(resultCode == RESULT_OK){

Uri uri = null;
if(imageReturnedIntent == null){ //since we used EXTRA_OUTPUT for camera, so it will be null

for(int i=0;i<cameraImageFiles.size();i++){
if(cameraImageFiles.get(i).exists()){
uri = Uri.fromFile(cameraImageFiles.get(i));
break;
}
}
}
else { // from gallery
uri = imageReturnedIntent.getData();
}

if(uri != null){
try {
Bitmap bitmap3 = decodeSampledBitmapFromResource(uri, 500, 500);
selectedImageBitmap = bitmap3;

Intent i= new Intent(this, CreatePropertyActivity51.class);
i.putExtra("photoUri", uri);
startActivityForResult(i,IMAGE_DESCRIPTION);

//*** show activity51

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

public Bitmap decodeSampledBitmapFromResource(Uri uri, int reqWidth, int reqHeight) throws IOException {

ContentResolver cr = getContentResolver();
InputStream inStream = cr.openInputStream(uri);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inStream, null, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

inStream.close();
inStream = cr.openInputStream(uri);


// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap result = BitmapFactory.decodeStream(inStream, null , options);
inStream.close();
return result;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize;
}
}


The second activity:

public class CreatePropertyActivity51 extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_property_51);
ImageView imageView = (ImageView) findViewById(R.id.img_selected_image);
Uri uri = getIntent().getParcelableExtra("photoUri");
Bitmap bitmap3;
try {
bitmap3 = decodeSampledBitmapFromResource(uri, 200, 200);
imageView.setImageBitmap(bitmap3);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.create_property_activity51, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public void sendPhoto(View v){
Intent data = new Intent();
data.putExtra("key", ((EditText)findViewById(R.id.etxt_photo_description)).getText().toString());
setResult(Activity.RESULT_OK, data);
finish();
}
public void cancelSendPhoto(View v){
Intent data = new Intent();
setResult(Activity.RESULT_CANCELED, data);
finish();
}


public Bitmap decodeSampledBitmapFromResource(Uri uri, int reqWidth, int reqHeight) throws IOException {

ContentResolver cr = getContentResolver();
InputStream inStream = cr.openInputStream(uri);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inStream, null, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

inStream.close();
inStream = cr.openInputStream(uri);


// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap result = BitmapFactory.decodeStream(inStream, null , options);
inStream.close();
return result;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize;
}
}


LogCat:


12-08 17:14:50.448: D/dalvikvm(12330): GC_FOR_ALLOC freed 619K, 16% free 19947K/23495K, paused 20ms, total 25ms 12-08 17:14:52.003:


D/dalvikvm(12330): GC_FOR_ALLOC freed 942K, 15% free 20023K/23495K,
paused 20ms, total 20ms 12-08 17:14:52.003: I/dalvikvm-heap(12330):
Grow heap (frag case) to 21.958MB for 2092136-byte allocation 12-08
17:14:52.050: D/dalvikvm(12330): GC_CONCURRENT freed 0K, 14% free
22066K/25543K, paused 12ms+4ms, total 42ms 12-08 17:14:53.940:
D/dalvikvm(12330): GC_FOR_ALLOC freed 1021K, 18% free 21045K/25543K,
paused 19ms, total 19ms 12-08 17:14:53.948: I/dalvikvm-heap(12330):
Grow heap (frag case) to 22.560MB for 1675864-byte allocation 12-08
17:14:53.971: D/dalvikvm(12330): GC_FOR_ALLOC freed 0K, 17% free
22681K/27207K, paused 21ms, total 21ms 12-08 17:14:53.987:
D/dalvikvm(12330): GC_FOR_ALLOC freed 0K, 17% free 22681K/27207K,
paused 19ms, total 19ms 12-08 17:14:53.995: I/dalvikvm-heap(12330):
Grow heap (frag case) to 24.719MB for 2263881-byte allocation 12-08
17:14:54.026: D/dalvikvm(12330): GC_FOR_ALLOC freed 0K, 16% free
24892K/29447K, paused 31ms, total 31ms 12-08 17:14:54.089:
D/dalvikvm(12330): GC_CONCURRENT freed 0K, 16% free 24892K/29447K,
paused 17ms+14ms, total 62ms 12-08 17:14:55.643: D/dalvikvm(12330):
GC_FOR_ALLOC freed <1K, 16% free 24893K/29447K, paused 20ms, total
20ms 12-08 17:14:55.667: I/dalvikvm-heap(12330): Grow heap (frag case)
to 29.039MB for 4527746-byte allocation 12-08 17:14:55.721:
D/dalvikvm(12330): GC_CONCURRENT freed 0K, 14% free 29315K/33927K,
paused 13ms+7ms, total 51ms 12-08 17:14:55.721: D/dalvikvm(12330):
WAIT_FOR_CONCURRENT_GC blocked 20ms 12-08 17:14:55.737:
I/Choreographer(12330): Skipped 652 frames! The application may be
doing too much work on its main thread. 12-08 17:14:55.987:
D/SensorManager(12330): unregisterListener:: Trklfufi 9
budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)ad)fb(
12-08 17:14:55.987: D/Sensors(12330): Remain listener = Sending ..
normal delay 200ms 12-08 17:14:55.987: I/Sensors(12330): sendDelay ---
200000000 12-08 17:14:55.987: D/SensorManager(12330): JNI - sendDelay
12-08 17:14:55.987: I/SensorManager(12330): Set normal delay = true
12-08 17:14:56.120: W/IInputConnectionWrapper(12330): getSelectedText
on inactive InputConnection 12-08 17:14:56.120:
W/IInputConnectionWrapper(12330): setComposingText on inactive
InputConnection 12-08 17:14:56.120: W/IInputConnectionWrapper(12330):
getExtractedText on inactive InputConnection 12-08 17:14:56.643:
D/dalvikvm(12330): GC_CONCURRENT freed 6893K, 28% free 24437K/33927K,
paused 12ms+16ms, total 64ms 12-08 17:14:57.010: D/dalvikvm(12330):
GC_FOR_ALLOC freed 1102K, 29% free 24331K/33927K, paused 25ms, total
25ms 12-08 17:14:57.425: D/dalvikvm(12330): GC_FOR_ALLOC freed 994K,
27% free 24781K/33927K, paused 21ms, total 21ms 12-08 17:14:57.464:
D/dalvikvm(12330): GC_CONCURRENT freed 5K, 21% free 26807K/33927K,
paused 12ms+4ms, total 35ms 12-08 17:14:57.964: D/AbsListView(12330):
Get MotionRecognitionManager 12-08 17:14:58.120: D/dalvikvm(12330):
GC_FOR_ALLOC freed 1498K, 25% free 25505K/33927K, paused 39ms, total
39ms 12-08 17:14:58.135: I/dalvikvm-heap(12330): Grow heap (frag case)
to 28.284MB for 3110734-byte allocation 12-08 17:14:58.167:
D/dalvikvm(12330): GC_FOR_ALLOC freed 1K, 23% free 28541K/36999K,
paused 32ms, total 32ms 12-08 17:14:58.190: W/ResourceType(12330):
Failure getting entry for 0x010802c0 (t=7 e=704) in package 0 (error
-75) 12-08 17:14:58.214: D/dalvikvm(12330): GC_CONCURRENT freed 2034K, 29% free 26521K/36999K, paused 16ms+7ms, total 45ms 12-08
17:14:59.245: D/dalvikvm(12330): GC_FOR_ALLOC freed 215K, 29% free
26591K/36999K, paused 22ms, total 22ms 12-08 17:14:59.245:
I/dalvikvm-heap(12330): Grow heap (frag case) to 30.828MB for
4666096-byte allocation 12-08 17:14:59.300: D/dalvikvm(12330):
GC_CONCURRENT freed 1K, 16% free 31146K/36999K, paused 32ms+4ms, total
57ms 12-08 17:14:59.300: D/dalvikvm(12330): WAIT_FOR_CONCURRENT_GC
blocked 24ms 12-08 17:15:00.495: W/IInputConnectionWrapper(12330):
showStatusIcon on inactive InputConnection 12-08 17:15:01.526:
D/dalvikvm(12330): GC_FOR_ALLOC freed 3339K, 25% free 28107K/36999K,
paused 22ms, total 24ms 12-08 17:15:01.526: D/AbsListView(12330):
[unregisterDoubleTapMotionListener] 12-08 17:15:01.534:
I/dalvikvm-heap(12330): Grow heap (frag case) to 32.233MB for
4586988-byte allocation 12-08 17:15:01.604: D/dalvikvm(12330):
GC_FOR_ALLOC freed 2K, 22% free 32584K/41479K, paused 71ms, total 71ms
12-08 17:15:01.620: I/MotionRecognitionManager(12330):

.unregisterListener : / listener count = 0->0, ubvf
9budiwrd5ordgfl5BakTrklMrfo$,@,)aa):88 12-08 17:15:01.643:
D/dalvikvm(12330): GC_CONCURRENT freed 18K, 22% free 32567K/41479K,
paused 13ms+4ms, total 40ms 12-08 17:15:01.643: D/dalvikvm(12330):
WAIT_FOR_CONCURRENT_GC blocked 26ms 12-08 17:15:01.690:
D/dalvikvm(12330): GC_FOR_ALLOC freed 4657K, 33% free 27910K/41479K,
paused 45ms, total 45ms 12-08 17:15:01.690: I/dalvikvm-heap(12330):
Grow heap (frag case) to 29.853MB for 2293502-byte allocation 12-08
17:15:01.729: D/dalvikvm(12330): GC_CONCURRENT freed 0K, 28% free
30150K/41479K, paused 4ms+4ms, total 39ms 12-08 17:15:01.729:
D/dalvikvm(12330): WAIT_FOR_CONCURRENT_GC blocked 6ms 12-08
17:15:23.596: D/AndroidRuntime(12330): Shutting down VM 12-08
17:15:23.596: W/dalvikvm(12330): threadid=1: thread exiting with
uncaught exception (group=0x41d0e2a0) 12-08 17:15:23.635:
E/AndroidRuntime(12330): FATAL EXCEPTION: main 12-08 17:15:23.635:
E/AndroidRuntime(12330): java.lang.RuntimeException: Unable to resume
activity
{com.appName.appName/com.appName.appName.CreatePropertyActivity5}:
java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=100, result=-1, data=null} to activity
{com.appName.appName/com.appName.appName.CreatePropertyActivity5}:
java.lang.NullPointerException 12-08 17:15:23.635:
E/AndroidRuntime(12330): at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2636)
12-08 17:15:23.635: E/AndroidRuntime(12330): at
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2664)
12-08 17:15:23.635: E/AndroidRuntime(12330): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2137)
12-08 17:15:23.635: E/AndroidRuntime(12330): at
android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3573)
12-08 17:15:23.635: E/AndroidRuntime(12330): at
android.app.ActivityThread.access$800(ActivityThread.java:140) 12-08
17:15:23.635: E/AndroidRuntime(12330): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
12-08 17:15:23.635: E/AndroidRuntime(12330): at
android.os.Handler.dispatchMessage(Handler.java:99) 12-08
17:15:23.635: E/AndroidRuntime(12330): at
android.os.Looper.loop(Looper.java:137) 12-08 17:15:23.635:
E/AndroidRuntime(12330): at
android.app.ActivityThread.main(ActivityThread.java:4918) 12-08
17:15:23.635: E/AndroidRuntime(12330): at
java.lang.reflect.Method.invokeNative(Native Method) 12-08
17:15:23.635: E/AndroidRuntime(12330): at
java.lang.reflect.Method.invoke(Method.java:511) 12-08 17:15:23.635:
E/AndroidRuntime(12330): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
12-08 17:15:23.635: E/AndroidRuntime(12330): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 12-08
17:15:23.635: E/AndroidRuntime(12330): at
dalvik.system.NativeStart.main(Native Method) 12-08 17:15:23.635:
E/AndroidRuntime(12330): Caused by: java.lang.RuntimeException:
Failure delivering result ResultInfo{who=null, request=100, result=-1,
data=null} to activity
{com.appName.appName/com.appName.appName.CreatePropertyActivity5}:
java.lang.NullPointerException 12-08 17:15:23.635:
E/AndroidRuntime(12330): at
android.app.ActivityThread.deliverResults(ActivityThread.java:3202)
12-08 17:15:23.635: E/AndroidRuntime(12330): at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2623)
12-08 17:15:23.635: E/AndroidRuntime(12330): ... 13 more 12-08
17:15:23.635: E/AndroidRuntime(12330): Caused by:
java.lang.NullPointerException 12-08 17:15:23.635:
E/AndroidRuntime(12330): at
com.appName.appName.CreatePropertyActivity5.onActivityResult(CreatePropertyActivity5.java:167)
12-08 17:15:23.635: E/AndroidRuntime(12330): at
android.app.Activity.dispatchActivityResult(Activity.java:5369) 12-08
17:15:23.635: E/AndroidRuntime(12330): at
android.app.ActivityThread.deliverResults(ActivityThread.java:3198)
12-08 17:15:23.635: E/AndroidRuntime(12330): ... 14 more

Answer Source

That's what I expected. Android is killing your process while the Camera or Gallery is running because it needs the resources. When the user returns to your app, Android has started a new process and is recreating your activities. What you'll need to do is override onSaveInstanceState() and onRestoreInstanceState() and save and restore anything that needs to be preserved while the user is playing with the camera or gallery app.