从相机拍摄的图像无法显示(的onActivityResult) [英] Image captured from the camera is not showing (OnActivityResult)

查看:161
本文介绍了从相机拍摄的图像无法显示(的onActivityResult)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能找回我刚才拍摄的照片。我相信它的东西错了我的onActivityResult,案例CAPTURE_IMAGE_ACTIVITY_REQUEST_ code。通过案例1从相册中的照片检索工作正常。请kindy建议。

I'm not able to retrieve the photo I have just captured. I believe its something wrong with my OnActivityResult, case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE. Retrieving photo from album by case 1 works fine. Please kindy advice.

来源$ C ​​$ C:

Source Code:

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

}

推荐答案

不管是不是你得到的图像是依赖于手机不幸的制造商。每一个Android版本,每一个制造商似乎有一个稍微不同的行为。有时,URL在结果为数据参数提供的(这是你在做什么),但有时它只是空的。我甚至有一个手机一旦返回RESULT_CANCEL尽管它完美地工作。

Whether or not you get the image is dependent on the manufacturer of the phone unfortunately. Every Android version and every Manufacturer seems to have a slightly different behavior. Sometimes the URL is provided in the result as "data" parameter (that's what you're doing), but sometimes it's just empty. I even got a phone once that returned RESULT_CANCEL although it worked perfectly.

,因为它似乎还有围绕实现各种不同的回退的没有办法。这里是我的code我使用的是目前在我们的项目之一。该成员变量 mTargetUri 需要设置为您提供的意图参数输出文件名 MediaStore.EXTRA_OUTPUT mCaptureTime 应该就是开始之前意向设置为 Sytem.currentTimeMillis()

As it seems there's no way around implementing all kind of different fallbacks. Here's my code I'm using currently in one of our projects. The member variable mTargetUri needs to be set to the output file name you provided as Intent parameter MediaStore.EXTRA_OUTPUT, mCaptureTime should be set to Sytem.currentTimeMillis() right before launching the 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 http://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.");
                    }
                }
            }
            }
    }
}

在code从这里#2和其他网站不同来源的起源。的基本思路是,我们

The code originates from different sources here on Stackoverflow and other sites. The basic idea is that we


  1. 忽略的结果code( RESULT_OK RESULT_CANCELED ),因为它们都没有迹象显示无论是照片是取并存储在SD卡或没有。在code以上只是记录的价值,但继续在任何情况下。

  2. 然后我们检查是否有被意图的结果提供了一个URI。如果我们得到一个URI那么它是安全的假设,它实际上将指向新拍摄的照片。但是,我们不会得到这样的URI虽然一切成功完美。为了处理我们只需使用URI我们最初提供相机的活动这种情况下( mTargetUri ),希望我们会找到新的文件在那里。

  3. 在一些较新的设备上的相机应用会自动添加新照片到媒体库中,然后创建中,我们在 targetUri 最初指定的方式第二个副本的名称。这种方法的问题是,我们最终得到重复的图像。为了解决这个问题,我们遵循从以下问题#1得出的逻辑:与ACTION_IM​​AGE_CAPTURE采取图像始终ExifInterface.TAG_ORIENTATION返回1对一些较新的设备。我不会去到很多细节在这里(只需按照链接以获得更好的理解),但基本的想法是,我们查询了Android媒体商店并检查它是否已经包含了我们通过比较时间戳( mCaptureTime 设置为 System.currentTimeMillis的()当捕获意图最初启动)。

  4. 在情况下,我们已经找到了媒体商店的入口,我们只是删除重复的。

  1. disregard the result code (RESULT_OK or RESULT_CANCELED) as they are no indication whether a photo was taken and stored on the SD card or not. The code above just logs the value but continues in any case.
  2. Then we check if there was an URI provided by the intent result. If we get an URI then it's safe to assume that it will actually point to the newly taken photo. However, we won't get such an URI although everything succeeded perfectly. To handle that case we simply use the URI we've originally provided to the camera activity (mTargetUri) hoping that we'll find the new file there.
  3. On some newer devices the camera app automatically adds a new photo to the media library and then creates a second copy names in the way we originally specified in targetUri. The problem with this approach is that we end up with duplicate images. To address this we follow a logic that is derived from the following Stackoverflow question: Images taken with ACTION_IMAGE_CAPTURE always returns 1 for ExifInterface.TAG_ORIENTATION on some newer devices. I won't go into many details here (just follow the link to get a better understanding) but the basic idea is that we query the Android media store and check if it already contains our new photo by comparing timestamps (mCaptureTime is set to System.currentTimeMillis() when the capture intent was originally started).
  4. In case we have found an entry in the media store we simply delete the duplicate.

这个逻辑应该在所有情况下工作,几乎所有的Andr​​oid设备在那里。我们已经测试了它在不同的设备和Android版本没有到目前为止其他问题一打。

This logic should work in all cases with virtually any Android device out there. We have tested it on a dozen of different devices and Android versions without additional issues so far.

这篇关于从相机拍摄的图像无法显示(的onActivityResult)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆