Android – NullPointerException in onactivityresult after calling camera intent

This is my question: I need to save photos to the default camera location instead of copying photos to my private location, adding GPS tags for copying, and deleting the original photos. Why? Because on some devices, when mediastore.extra is used in the request_ When output, the photos are saved in the default location anyway. Now, when the application is close to completion and tested on the new device (Motorola mb526), it will crash after taking photos. The same thing happens on the simulator

The problem is that in onactivityresult (int requestcode, int resultcode, intent data), data. GetData () returns null

So my question is - is there a unified way to save pictures at a given location and elsewhere?

Edit:

Capture image intent

protected void takePhotos() {
    Intent imageIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

And results

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK && data != null) {
                Uri capturedImageUri = data.getData();
                Log.d("cameraResult", "data =" + data.getDataString());
                String capturedPicFilePath = getRealPathFromURI(capturedImageUri);
                writeImageData(capturedImageUri, capturedPicFilePath);
            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Picture not taken",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Picture not taken",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}

public String getRealPathFromURI(Uri contentUri) {
    String[] projx = { MediaStore.Images.Media.DATA };
    Cursor cursor;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
        CursorLoader loader = new CursorLoader(this, contentUri, projx,
                null, null, null);
        cursor = loader.loadInBackground();
    } else {
        cursor = this.managedQuery(contentUri, projx, null, null, null);
    }
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Logcat output:

07-23 18:42:26.486: D/cameraResult(1744): data =null
07-23 18:42:26.486: D/AndroidRuntime(1744): Shutting down VM
07-23 18:42:26.486: W/dalvikvm(1744): threadid=1: thread exiting with uncaught exception (group=0xb70334f0)
07-23 18:42:26.496: E/AndroidRuntime(1744): FATAL EXCEPTION: main
07-23 18:42:26.496: E/AndroidRuntime(1744): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1513, result=-1, data=Intent { act=inline-data (has extras) }} to activity {arios.e_gps/arios.e_gps.photos.PhotoListAct}: java.lang.NullPointerException
07-23 18:42:26.496: E/AndroidRuntime(1744):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at android.os.Looper.loop(Looper.java:130)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at java.lang.reflect.Method.invokeNative(Native Method)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at java.lang.reflect.Method.invoke(Method.java:507)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at dalvik.system.NativeStart.main(Native Method)
07-23 18:42:26.496: E/AndroidRuntime(1744): Caused by: java.lang.NullPointerException
07-23 18:42:26.496: E/AndroidRuntime(1744):     at arios.e_gps.photos.PhotoListAct.getRealPathFromURI(PhotoListAct.java:316)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at arios.e_gps.photos.PhotoListAct.onActivityResult(PhotoListAct.java:165)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
07-23 18:42:26.496: E/AndroidRuntime(1744):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
07-23 18:42:26.496: E/AndroidRuntime(1744):     ... 11 more

resolvent:

edit

This may be wrong, but I'm in action_ IMAGE_ After capture, I did this to get the URI, which is useful to me

        Bitmap picture = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        picture.compress(Bitmap.CompressFormat.JPEG, 100, stream);

        String path = Images.Media.insertImage(getContentResolver(), picture,
                "title" , null);
        Uri uriPhoto = Uri.parse(path);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>