android – 调用相机意图后onActivityResult中的NullPointerException

这是我的问题:我需要将照片保存到认的相机位置,而不是将照片复制到我的私人位置,添加GPS标签进行复制,以及删除原始照片.为何如此?因为在某些设备上,当在请求中使用MediaStore.EXTRA_OUTPUT时,照片无论如何都保存在认位置.现在,当应用程序接近完成时,在新设备(摩托罗拉MB526)上进行测试时,它会在接受拍摄照片后崩溃.同样的事情发生在模拟器上.

问题是,在onActivityResult(int requestCode,int resultCode,Intent data)中,data.getData()返回null.

所以我的问题是 – 有没有统一的方法来保存给定位置和其他地方的图片

编辑:

捕获图像意图

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

和结果

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输出

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

解决方法:

编辑

这可能是错的,但我在ACTION_IMAGE_CAPTURE之后做了这个以获得uri,它对我有用.

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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...