在棒棒糖中通过相机捕获照片后获取onActivityResult()方法中的null意图

时间:2016-01-20 09:59:27

标签: android android-intent intentfilter android-camera-intent

在棒棒糖和更高版本中从相机捕获图像时,onActivityResult上的结果数据为null。

launchCamera();

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        ((MyWebViewActivity) context).fileUri = CommonUtils.getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, ((MyWebViewActivity) context).fileUri);
        ((MyWebViewActivity) context).startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

@覆盖

onActivityResult(int requestCode,int resultCode,Intent data);

if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            if(fileUri!=null){
                file_path = fileUri.getPath();
                System.out.println("file-path---=" + file_path);
            }else{
                Toast.makeText(this, "Please try again", Toast.LENGTH_LONG).show();
            }
            if(!CommonUtils.isEmpty(file_path)){

            }
        } else if (resultCode == RESULT_CANCELED) {
            // "User cancelled image capture",
        } else {
            // failed to capture image
        }
    } 

getOutputMediaFileUri()

File file = getOutputMediaFile(type);
    if (file != null) {
        return Uri.fromFile(file);
    }
    return null;

getOutputMediaFile()

// External sdcard location
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "My Images");
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;
    System.out.println("mediaStorageDir==" + mediaStorageDir);
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".png");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }
    return mediaFile;

1 个答案:

答案 0 :(得分:0)

看起来您正在为片段的结果启动活动。在这种情况下,您必须覆盖片段和Activity中的OnActivityResult()方法。 看看这个答案。这应该有所帮助 https://stackoverflow.com/a/21826858/7659504