android camera causing null pointer exception

2019-02-07 19:57发布

问题:

My app has null pointer exceptions when it tries to take a picture and with the camera. My app calls the camera thusly:

        //go to camera app.

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile()));

        //open camera. Take picture. 

        startActivityForResult(intent, PIC_ONE);

and the onresult code is:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == PIC_ONE)
            {



                    //prevent null pointer
                    if(data.getData()!=null){
                        uri1 = data.getData();
                        File f = new File(getPath(uri1));//<-NULL POINTER EXCEPTION
                        //...do stuff
    }

This causes a nullpointer exception, as shown above. getPath causes this null pointer. getPath() is shown below. This method is not retrieving the image from the mediastore as it should.

public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        startManagingCursor(cursor);
        if(cursor!=null){
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return  cursor.getString(column_index);
        }
        else return null;
    }

Below is the logcat:

    06-07 22:53:59.650: ERROR/AndroidRuntime(3935): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data dat=file:///mnt/sdcard/image.tmp typ=image/jpeg (has extras) }} to activity {com.myapp.myactivity/myactivity.java}: java.lang.NullPointerException
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2553)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at android.app.ActivityThread.access$2000(ActivityThread.java:121)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at android.os.Looper.loop(Looper.java:138)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at android.app.ActivityThread.main(ActivityThread.java:3701)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at java.lang.reflect.Method.invoke(Method.java:507)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at dalvik.system.NativeStart.main(Native Method)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935): Caused by: java.lang.NullPointerException
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at java.io.File.fixSlashes(File.java:205)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at java.io.File.init(File.java:189)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at java.io.File.<init>(File.java:139)
06-07 22:53:59.650: ERROR/AndroidRuntime(3935):     at com.myapp.myactivity.java.onActivityResult(myactivity.java:928)

The strange thing is this app works fine on an Xperia X10, but on newer phones including Xperia acro it has this null pointer. I have researched it and it seems the problem could be caused because the camera app is too chunky and causes the initial activity to finish.

So I overrode these two methods:

 @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (uri1 != null) {
            outState.putString("cameraImageUri1", uri1.toString());
        }

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState.containsKey("cameraImageUri1")) {
            uri1 = Uri.parse(savedInstanceState.getString("cameraImageUri1"));
        }


        Log.v("THIS ACTIVITY","has called onrestore...");
    }

onRestoreInstanceState is never even called during the picture taking and finishing process. So I dont know what is happening. Why is this null pointer being called

回答1:

        if (data != null) {
                File myFile;

                Uri selectedImageUri = data.getData();
                String filemanagerstring = selectedImageUri.getPath();
                String selectedImagePath = getPath(selectedImageUri);
                if (selectedImagePath != null)
                    myFile = new File(selectedImagePath);
                else if (filemanagerstring != null)
                    myFile = new File(filemanagerstring);
                 } 

//here getPath method

public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }


回答2:

Try this.

public String getPath(Uri photoUri) {

 String filePath ="";
    if (photoUri != null){
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            filePath = cursor.getString(columnIndex);
            cursor.close();
      }
  return filePath;
 }