android摄像头捕获并获取uri

时间:2016-02-15 02:56:26

标签: android file camera capture

我的应用程序在除nexus 5之外的所有设备中正确运行!为什么? 在nexus 5中有错误

Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/DCIM/IMG_20160215_061354.jpg: open failed: EACCES (Permission denied)

我的代码:

 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            mImageCaptureUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri); // set the image file name
            startActivityForResult(intent, PICK_FROM_CAMERA);

我在清单文件中的权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<!--<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"/>-->
<uses-permission android:name="android.permission.READ_MEDIA_STORAGE"/>
<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

在活动结果上只显示在nexus 5中的图像视图中的图片

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
       if (requestCode == PICK_FROM_CAMERA) {
            img.setImageURI(mImageCaptureUri);              
        }
    }
}

从developer.google中复制两个功能

    private 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) {
    File mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }
    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 if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

0 个答案:

没有答案