onActivityResult方法中的错误

时间:2012-03-01 11:46:47

标签: android sqlite android-camera

我仍有同样的问题..

Java代码:

public void takePic() {
    String fileName = "temp.jpg";  
    ContentValues values = new ContentValues();  
    values.put(MediaStore.Images.Media.TITLE, fileName);  
    mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

    cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
    startActivityForResult(cameraIntent, Camera_requestCode);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Camera_requestCode && resultCode == RESULT_OK) {
        String[] projection = { MediaStore.Images.Media.DATA}; 
        Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
        int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
        cursor.moveToFirst(); 
        String capturedImageFilePath = cursor.getString(column_index_data);

        insertPath(capturedImageFilePath);
        Bitmap image = (Bitmap) data.getExtras().get("data");
    }
};

logcat的:

    03-01 13:36:55.390: E/AndroidRuntime(20416): FATAL EXCEPTION: main
03-01 13:36:55.390: E/AndroidRuntime(20416): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.android.SaveCameraPictureDemo/com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity}: java.lang.NullPointerException
03-01 13:36:55.390: E/AndroidRuntime(20416):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at android.app.ActivityThread.access$2000(ActivityThread.java:117)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at android.os.Looper.loop(Looper.java:123)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at android.app.ActivityThread.main(ActivityThread.java:3691)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at java.lang.reflect.Method.invokeNative(Native Method)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at java.lang.reflect.Method.invoke(Method.java:507)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at dalvik.system.NativeStart.main(Native Method)
03-01 13:36:55.390: E/AndroidRuntime(20416): Caused by: java.lang.NullPointerException
03-01 13:36:55.390: E/AndroidRuntime(20416):    at com.android.SaveCameraPictureDemo.SaveCameraPictureDemoActivity.onActivityResult(SaveCameraPictureDemoActivity.java:73)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at android.app.Activity.dispatchActivityResult(Activity.java:3908)
03-01 13:36:55.390: E/AndroidRuntime(20416):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
03-01 13:36:55.390: E/AndroidRuntime(20416):    ... 11 more

2 个答案:

答案 0 :(得分:1)

下面的行是错误的,你不能像这样获得捕获图像的路径。

String imgPath = data.getData().toString();
        insertPath(imgPath);

答案 1 :(得分:0)

U永远不会从其他活动中获取位图。那是你的问题 为什么不简单地传递图像的路径,让第二个活动决定如何处理该路径(在这种情况下解码并显示图像)。