Problem with showing camera image: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity

  • Replies:1
Susanne Hou
  • Forum posts: 1

Mar 9, 2016, 4:20:14 PM via Website

Hey guys. I'm fairly new to android development, and a few days ago I ran into a problem that i still haven't been able to solve.

What I am working on is an activity where the user can press a button to take a picture. This picture will then be displayed after it has been taken. See image below.

image

However, I can't find a way to show the image. The camera is opened and you can take a picture - but then when you press "done" in the camera, the app crashes. I've posted the error log below the code. `

package com.susanne.firsthandin;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CameraActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private EditText t1, t2;
private ImageButton returnbutton;
private ImageButton takepicturebutton;
private ImageView cameraimage;
String mCurrentPhotoPath;
static final int REQUEST_TAKE_PHOTO = 1;
static final int REQUEST_IMAGE_CAPTURE = 1;
boolean shouldExecuteOnResume;
SharedPreferences camerapreferences;
Intent implicitCameraIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    if(savedInstanceState != null)
    {
        Log.d(TAG, "onCreate() Restoring previous state");
    }
    else{
        Log.d(TAG, "OnCreate() No Saved State Available");
    }

    shouldExecuteOnResume = false;


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    returnbutton = (ImageButton)findViewById(R.id.ReturnFromCameraButton);
    returnbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            finish();
        }
    });

    final File mypicturedirectory = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);


    takepicturebutton = (ImageButton)findViewById(R.id.TakePictureButton);
    takepicturebutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String filename = mypicturedirectory.getPath()+File.separator+"IMG"+timestamp+".jpg";
            File imagefile = new File(filename);
            Uri imageuri = Uri.fromFile(imagefile);


            camerapreferences = getSharedPreferences("camerapreferences", MODE_PRIVATE);
            SharedPreferences.Editor preferenceEditor;
            preferenceEditor = camerapreferences.edit();

            preferenceEditor.putString("filepath", filename);

            preferenceEditor.apply();

            implicitCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //implicitCameraIntent.setData(imageuri);
            implicitCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageuri);
            if (implicitCameraIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(implicitCameraIntent, REQUEST_IMAGE_CAPTURE);
            }
        }
    });

    t1 = (EditText)findViewById(R.id.enterName);
    t2 = (EditText)findViewById(R.id.enterimagedescription);
    cameraimage = (ImageView)findViewById(R.id.cameraimage);


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK ) {


        Bundle bundle = data.getExtras();
        //bundle.getParcelable();
        SharedPreferences getfilepref = getSharedPreferences("camerapreferences", MODE_PRIVATE);
        String filename = getfilepref.getString("filepath", "");


        setPictureToImageView(filename, cameraimage);
    }
}
@Override
protected void onPause() {
    super.onPause();
    Log.d(TAG, "The onResume() event");

    String imgName = t1.getText().toString();
    String desc = t2.getText().toString();

    SharedPreferences.Editor preferenceEditor;
    camerapreferences = getPreferences(MODE_PRIVATE);
    preferenceEditor = camerapreferences.edit();

    preferenceEditor.putString("name", imgName);
    preferenceEditor.putString("description", desc);
    //preferenceEditor.putString("filepath", mCurrentPhotoPath);

    preferenceEditor.apply();


}

protected void onResume() {
    super.onResume();
    Log.d(TAG, "The onResume() event");

    SharedPreferences camerapreferences = getPreferences(MODE_PRIVATE);
    String imgName = camerapreferences.getString("name", "no name");
    String desc = camerapreferences.getString("description", "no description");
    String image = camerapreferences.getString("filepath", null);


    t1.setText(imgName);
    t2.setText(desc);

    if(shouldExecuteOnResume)
    {
        setPictureToImageView(image, cameraimage);
    }
    else
    {
        shouldExecuteOnResume = true;
    }

}

public void setPictureToImageView(String filename, ImageView iv)
{
    int targetW = iv.getWidth();
    int targetH = iv.getHeight();

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scalefactor = Math.min(photoW/targetW, photoH/targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scalefactor;

    Bitmap bitmap = BitmapFactory.decodeFile(filename, bmOptions);

    iv.setRotation(90);
    iv.setImageBitmap(bitmap);
}

public void OnStart() {
    super.onStart();
    Log.d(TAG, "The onStart() event");
}

public void OnRestart()
{
    super.onRestart();
    Log.d(TAG, "The onRestart() event");
}

public void OnStop()
{
    super.onStop();
    Log.d(TAG, "The onStop() event");
}

public void OnDestroy()
{
    super.onDestroy();
    Log.d(TAG, "The onDestroy() event");
}

}
`

Error log:

FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.susanne.firsthandin/com.susanne.firsthandin.CameraActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
at android.app.ActivityThread.access$1100(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.susanne.firsthandin.CameraActivity.onActivityResult(CameraActivity.java:107)
at android.app.Activity.dispatchActivityResult(Activity.java:5192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184) 
at android.app.ActivityThread.access$1100(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
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:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

Reply
Philipp Eichhorn
  • Forum posts: 12

Mar 10, 2016, 3:42:06 PM via Website

Its hard to debug this without having line numbers (where is 107?). Anyways, you are getting a NullPointerException (trying to access an object which is null --> Exception) somewhere in the onResume method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK ) {

    Bundle bundle = data.getExtras();
    SharedPreferences getfilepref = getSharedPreferences("camerapreferences", MODE_PRIVATE);
    String filename = getfilepref.getString("filepath", "");

    setPictureToImageView(filename, cameraimage);
  }
}

Just based on this I can see two locations where the exception might occur:

data.getExtras();

or

getfilepref.getString(...);

Likely either one of those is null.

Side note: taking photos in Android is not super easy, especially since there are so many different devices and a big pile of edge cases to really get it "right". Depending on what you are trying to do, try googeling for "Taking Photos Simply" which will lead you to a Android dev page. There they describe how to take photos using the default camera app.

Reply