2

我无法检索我刚刚拍摄的照片。我认为我的 OnActivityResult 有问题,案例 CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE。按案例 1 从相册中检索照片效果很好。请善意的建议。

源代码:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        super.onActivityResult(requestCode, resultCode, data); 
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
           if(resultCode == RESULT_OK){     
             Uri targetUri = (data != null) ? data.getData() : null;
             if (targetUri == null) {
                 targetUri = fileUri;
             }
             imageView.setImageURI(targetUri);
           }
           if(resultCode == RESULT_CANCELED){   
             Uri targetUri = (data != null) ? data.getData() : null;
             if (targetUri == null) {
                 targetUri = fileUri;
             }
             imageView.setImageURI(targetUri);
           }
           else{  ; 
             Uri targetUri = (data != null) ? data.getData() : null;
             if (targetUri == null) {
                 targetUri = fileUri;
             }
             imageView.setImageURI(targetUri);
           }
    }   
    else if(requestCode == 1) {
        if(resultCode == RESULT_OK){  
            Uri selectedImage = data.getData();
            imageView.setImageURI(selectedImage);
        }
    }
}

}

4

3 回答 3

2

不幸的是,您是否获得图像取决于手机制造商。每个 Android 版本和每个制造商的行为似乎都略有不同。有时 URL 在结果中作为“数据”参数提供(这就是你正在做的),但有时它只是空的。我什至有一部电话返回了 RESULT_CANCEL,尽管它运行良好。

似乎没有办法实现各种不同的后备。这是我目前在我们的一个项目中使用的代码。成员变量mTargetUri需要设置为您作为 Intent 参数提供的输出文件名MediaStore.EXTRA_OUTPUTmCaptureTimeSytem.currentTimeMillis()在启动 Intent 之前设置为正确。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Log.v(LOG_TAG, "Camera intent succeeded");
        }
        else if (resultCode == RESULT_CANCELED) {
            Log.i(LOG_TAG, "Camera intent aborted");
        }
        else {
            Log.e(LOG_TAG, "Camera intent failed with result code: " + resultCode);
        }

        // Regardless of the resultCode we're going to check if a new photo has been
        // created on the phone. At least on the Samsung Galaxy S3 the behavior
        // could be observed that although the result code was "0" the camera app
        // created two (!) files on the SD card.

        // Image captured and saved to fileUri specified in the Intent
        Uri targetUri = (data != null) ? data.getData() : null;
        if (targetUri == null) {
            Log.w(LOG_TAG, "Camera intent returned empty result.");
            targetUri = mTargetUri;
        }

        if (targetUri != null) {
            String targetFilePath = targetUri.getPath();
            File targetFile = new File(targetFilePath);
            if (targetFile.exists()) {
                Log.i(LOG_TAG, "Image saved to: " + targetUri.toString());

                // Fix for issue reported here: http://code.google.com/p/android/issues/detail?id=22822
                // and here: http://code.google.com/p/android/issues/detail?id=19268
                // We're following the proposed solution from https://stackoverflow.com/questions/8450539/
                int rotation = -1;
                long fileSize = targetFile.length();
                Cursor mediaCursor = getContentResolver().query(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                        new String[] { 
                            MediaStore.Images.ImageColumns.ORIENTATION, 
                            MediaStore.MediaColumns.SIZE,
                            MediaStore.MediaColumns.DATA }, 
                        MediaStore.MediaColumns.DATE_ADDED + ">=?", 
                        new String[] { String.valueOf(mCaptureTime/1000 - 1)}, 
                        MediaStore.MediaColumns.DATE_ADDED + " desc");

                if ((mediaCursor != null) && (mCaptureTime != 0)) {
                    if (mediaCursor.moveToFirst()) {
                        do {
                            long size = mediaCursor.getLong(1);
                            Uri uri = Uri.parse(mediaCursor.getString(2));
                            // Extra check to make sure that we are getting the orientation from the proper file
                            if (size == fileSize && !uri.equals(targetUri.toString())) {
                                rotation = mediaCursor.getInt(0);
                                break;
                            }
                        } while (mediaCursor.moveToNext());
                    }
                    mediaCursor.close();
                }

                if (rotation == -1) {
                    // It seems that there has been no duplication and no rotation issue so far. This means we can
                    // add our newly created file to the media library.
                    // TODO
                }
                else {
                    // Looks like the picture already exists in the media library. This indicates we got a duplicate.
                    Log.w(LOG_TAG, "Duplicate image found for " + targetUri.toString() + " in media library. Deleting the copy.");
                    if (!targetFile.delete()) {
                        Log.e(LOG_TAG, "Failed to delete duplicate image.");
                    }
                }
            }
            }
    }
}

代码来自 Stackoverflow 和其他网站上的不同来源。基本思想是我们

  1. 忽略结果代码 (RESULT_OKRESULT_CANCELED),因为它们不表示照片是否已拍摄并存储在 SD 卡上。上面的代码只是记录值,但在任何情况下都会继续。
  2. 然后我们检查是否有意图结果提供的 URI。如果我们得到一个 URI,那么可以安全地假设它实际上会指向新拍摄的照片。但是,尽管一切都非常成功,但我们不会得到这样的 URI。为了处理这种情况,我们只需使用我们最初提供给相机活动 ( mTargetUri) 的 URI,希望我们能在那里找到新文件。
  3. 在一些较新的设备上,相机应用程序会自动将新照片添加到媒体库,然后按照我们最初在targetUri. 这种方法的问题是我们最终会得到重复的图像。为了解决这个问题,我们遵循源自以下 Stackoverflow 问题的逻辑:使用 ACTION_IMAGE_CAPTURE 拍摄的图像在某些较新的设备上总是为 ExifInterface.TAG_ORIENTATION 返回 1。我不会在这里详细介绍(只需点击链接以获得更好的理解),但基本思想是我们查询 Android 媒体商店并通过比较时间戳检查它是否已经包含我们的新照片(mCaptureTime设置System.currentTimeMillis()为捕获意图最初是开始的)。
  4. 如果我们在媒体存储中找到了一个条目,我们只需删除重复项。

这种逻辑应该适用于几乎所有 Android 设备的所有情况。到目前为止,我们已经在十几种不同的设备和 Android 版本上对其进行了测试,没有出现其他问题。

于 2012-11-09T19:23:32.927 回答
2

我遇到了同样的问题,一旦检查你有没有放任何与历史相关的标签?

不要android:noHistory="true"在清单中放置标签

如果您android:noHistory="true"在清单内的活动中使用,它将在停止后从堆栈中删除。

注意:如果您使用的是选项卡活动,那么您也不应该使用android:noHistory="true"

或者简单地android:noHistory="false"在 manifest 中放入活动。

于 2013-07-03T10:47:35.137 回答
0

尝试转动相机并在风景模式下拍照。Mediastore Camera Intent 似乎只拍摄风景照片。

于 2012-11-27T08:11:26.577 回答