Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

android 4.4 video from camera gives security Exception

I am using following code for video recording using camera

code on record button click:-

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

code inside switch - case block of onActivityResult :-

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 method

private void videoFromCameraNexus(int resultCode, Intent data) {

        if(fileUri != null) {
            Log.d(TAG, "Video saved to:
" + fileUri);
            Log.d(TAG, "Video path:
" + fileUri.getPath());
            Log.d(TAG, "Video name:
" + getName(fileUri)); 
    // use uri.getLastPathSegment() if store in folder
    //use the file Uri.
        }
    }

Get the output Media file uri with the following Method

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;

}

Its works for me till android 4.3 os but on Android 4.4 I am getting java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider

My Logcat details are as follows :-

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should add the permission android.permission.READ_EXTERNAL_STORAGE to your manifest file by adding the following line to it :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...