android android.provider.MediaStore.ACTION_VIDEO_CAPTURE return null onActivityResult with nexus 7

后端 未结 3 858
春和景丽
春和景丽 2020-12-31 16:20

I am using intent for record video.

so i use following code on recordVideo button\'s click

Videofilepath = \"\";
Intent intent = new Intent(android.p         


        
相关标签:
3条回答
  • 2020-12-31 16:51

    Made a fork and fixed the problem by storing to a permanent directory which will still be available after the result has been received.

    https://github.com/pencilcheck/cordova-plugin-media-capture

    0 讨论(0)
  • 2020-12-31 16:56

    Finally I resolved this issue. Nexus 7 Stores the videos in DCIM directory but onActivityResults it returns null. Its an documented issue with Nexus 7 device.

    so fix this issue with intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    the code is as :-

    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:\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.
            }
        }
    

    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.

    0 讨论(0)
  • 2020-12-31 16:59

    Thanks for the workaround!

    Here is more brushed and copy-paste usable code:

    /**
     * Create intent to take video.
     */
    public static Intent createTakeVideoIntent() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        Uri uri = getOutputVideoUri();  // create a file to save the video in specific folder
        if (uri != null) {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        }
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
    
        return intent;
    }
    
    
    @CheckForNull
    private static Uri getOutputVideoUri() {
        if (Environment.getExternalStorageState() == null) {
            return null;
        }
    
        File mediaStorage = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "YOUR_APP_VIDEO");
        if (!mediaStorage.exists() &&
                !mediaStorage.mkdirs()) {
            Log.e(YourApplication.TAG, "failed to create directory: " + mediaStorage);
            return null;
        }
    
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
        File mediaFile = new File(mediaStorage, "VID_" + timeStamp + ".mp4");
        return Uri.fromFile(mediaFile);
    }
    

    Tested on Nexus 4 v4.3 JWR66Y

    0 讨论(0)
提交回复
热议问题