4

这是我的问题:我需要将照片保存到默认相机位置,而不是将其复制到我的私人位置,添加 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);
}

日志输出:

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
4

2 回答 2

3

请求缩略图时 Data.getData() 不为空。将图片记录到任何位置时,它将始终为空。

于 2013-07-23T20:40:48.430 回答
0

编辑

这可能是错误的,但我这样做是为了在 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);
于 2013-07-23T21:09:10.923 回答