Android 使用照相机拍照时,onActivityResult不包含媒体(照片)uri

Android 使用照相机拍照时,onActivityResult不包含媒体(照片)uri,android,camera,uri,media,Android,Camera,Uri,Media,嗨,我开始在安卓系统中使用摄像头,这是最简单的 问题是在我拍摄完照片后,由于某种原因,文件的uri只是空的 步骤: 我运行了takePhoto功能,手机的标准照相应用程序启动, 我拍照,我保存它 我的应用程序运行了onActivityResult。。。func,因为它从摄影机返回控件,并且文件的uri为空 代码: 在我的活动中: /** * Intent code for capturing a photo */ private static final int CAPTURE_IMAGE_

嗨,我开始在安卓系统中使用摄像头,这是最简单的

问题是在我拍摄完照片后,由于某种原因,文件的uri只是空的

步骤:

我运行了takePhoto功能,手机的标准照相应用程序启动, 我拍照,我保存它

我的应用程序运行了onActivityResult。。。func,因为它从摄影机返回控件,并且文件的uri为空

代码:

在我的活动中:

/**
 * Intent code for capturing a photo
 */
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

/**
 * 
 * Takes a photo
 */
public void takePhoto() {
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = CameraHelper.getOutputMediaFileUri(CameraHelper.MEDIA_TYPE_IMAGE); // create a file to save the image
    Log.i("PHOTO", "file uri is: " + fileUri.toString());
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
/**
 * 
 * After taking the photo...
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            if (data == null) {
                Toast.makeText(this, "Camera error: data is null!", Toast.LENGTH_LONG).show();
                Log.i("PHOTO", "Camera error: data is null!");
                // Error is here, data does not contains the uri.
            }
            else {
                Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
                Log.i("PHOTO", "Image saved to:\n" + data.getData());
            }
        }
        else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        }
        else {
            // Image capture failed, advise user
        }
    }
}
CameraHelper类:

public class CameraHelper {

public static final int MEDIA_TYPE_IMAGE = 1;



/** Create a file Uri for saving an image or video */
public static Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {

        Log.i("PHOTO", "directory is not exists yet!");
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
        else {
            Log.i("PHOTO", "just made directory: " + mediaStorageDir.getAbsolutePath());
        }
    }
    else {
        Log.i("PHOTO", "directory is already exist: " + mediaStorageDir.getAbsolutePath());
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    }
    else {
        return null;
    }
    return mediaFile;
}
}

日志输出:

01-13 16:15:37.485: I/PHOTO(6818): directory is already exist: /storage/sdcard0/Pictures/MyCameraApp
01-13 16:15:37.495: I/PHOTO(6818): file uri is: file:///storage/sdcard0/Pictures/MyCameraApp/IMG_20150113_161537.jpg


01-13 16:16:13.482: I/PHOTO(6818): Camera error: data is null!
所有图片都在文件夹中:pictures/MyCameraApp但onActivityResult。。。在数据字段中不包含uri


我做错了什么?

这里有一个很好的描述:不使用额外的输出,相机应用程序返回一个非空的意图,并在返回的意图中返回一个缩略图。如果您使用URI传递额外的_输出,它将返回一个空意图,并且图片位于您传入的URI中。