android相机导致空指针异常

时间:2012-06-07 14:14:49

标签: android android-intent camera nullpointerexception

我的应用在尝试拍照和使用相机时有空指针异常。 我的应用程序如此调用相机:

        //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);

并且onresult代码是:

@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
    }

这会导致nullpointer异常,如上所示。 getPath导致此空指针。 getPath()如下所示。 此方法不是应该从mediastore检索图像。

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;
    }

下面是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)

奇怪的是这个应用程序在Xperia X10上工作正常,但在包括Xperia acro的新手机上它有这个空指针。我已经研究了它,似乎问题可能是因为相机应用程序太粗糙而导致初始活动完成。

所以我改写了这两种方法:

 @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。所以我不知道发生了什么。为什么要调用这个空指针

2 个答案:

答案 0 :(得分:4)

        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);
                 } 

//这里是getPath方法

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;
    }

答案 1 :(得分:0)

试试这个。

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;
 }