我正在使用以下代码进行使用相机进行视频录制
记录按钮上的代码点击: -
intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFile(MEDIA_TYPE_VIDEO); // create a file to save the video in specific folder (this works for video only)
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
// start the Video Capture Intent
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED_NEXUS);
开关中的代码 - onActivityResult的case块: -
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_VIDEO_CAPTURED_NEXUS:
this.videoFromCameraNexus(resultCode, data);
break;
default:
break;
}
}
}
// videoFromCameraNexus方法
private void videoFromCameraNexus(int resultCode, Intent data) {
if(fileUri != null) {
Log.d(TAG, "Video saved to:\n" + fileUri);
Log.d(TAG, "Video path:\n" + fileUri.getPath());
Log.d(TAG, "Video name:\n" + getName(fileUri));
// use uri.getLastPathSegment() if store in folder
//use the file Uri.
}
}
使用以下方法获取输出媒体文件uri
public Uri getOutputMediaFile(int type)
{
// To be safe, you should check that the SDCard is mounted
if(Environment.getExternalStorageState() != null) {
// this works for Android 2.2 and above
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "SMW_VIDEO");
// 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()) {
if (! mediaStorageDir.mkdirs()) {
Log.d(TAG, "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return Uri.fromFile(mediaFile);
}
return null;
}
它适用于我直到android 4.3 os但在Android 4.4上我得到java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider
我的Logcat详细信息如下: -
11-27 19:34:31.157: E/AndroidRuntime(3876): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=210, result=-1, data=Intent { dat=content://media/external/video/media/132 }} to activity {com.sus.SUSV7_1.Activity/com.sus.SUSV7_1.Activity.ConnectMeActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/video/media/132 from pid=3876, uid=10088 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
11-27 19:34:31.157: E/AndroidRuntime(3876): at android.app.ActivityThread.deliverResults(ActivityThread.java:3346)
11-27 19:34:31.157: E/AndroidRuntime(3876): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3389)
11-27 19:34:31.157: E/AndroidRuntime(3876): at android.app.ActivityThread.access$1200(ActivityThread.java:135)
11-27 19:34:31.157: E/AndroidRuntime(3876): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445)
11-27 19:34:31.157: E/AndroidRuntime(3876): at android.os.Handler.dispatchMessage(Handler.java:102)
11-27 19:34:31.157: E/AndroidRuntime(3876): at android.os.Looper.loop(Looper.java:137)
11-27 19:34:31.157: E/AndroidRuntime(3876): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-27 19:34:31.157: E/AndroidRuntime(3876): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 19:34:31.157: E/AndroidRuntime(3876): at java.lang.reflect.Method.invoke(Method.java:515)
11-27 19:34:31.157: E/AndroidRuntime(3876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-27 19:34:31.157: E/AndroidRuntime(3876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-27 19:34:31.157: E/AndroidRuntime(3876): at dalvik.system.NativeStart.main(Native Method)
11-27 19:34:31.157: E/AndroidRuntime(3876): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/video/media/132 from pid=3876, uid=10088 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
11-27 19:34:31.157: E/AndroidRuntime(3876): at android.os.Parcel.readException(Parcel.java:1461)
答案 0 :(得分:8)
您应该通过向其添加以下行来将权限android.permission.READ_EXTERNAL_STORAGE
添加到您的清单文件中:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />