微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 使用新的存储访问框架(SAF)将视频录制到外部SD卡时出现IllegalStateException

我有以下方法,它适用于Android 4.3或更早版本的内部存储和外部SD卡:

    public void Recorder_Prepare() {        
        recorder = new MediaRecorder();
        recorder.setPreviewdisplay(holder.getSurface());
        mCamera.unlock();
        recorder.setCamera(mCamera);
        recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setProfile(camcorderProfile);
        recorder.setVideoSize(ResolutionWidth, ResolutionHeight);

        File DirectoryExistCheck = new File(VideoFolderAbsolutePATH);
        if(!DirectoryExistCheck.exists()) {
            DirectoryExistCheck.mkdir();
        }

        String VideoPath = VideoFolderAbsolutePATH + separator + "Video.mp4"; 
        File NewVideoFile = new File(VideoPath);
        recorder.setoutputFile(NewVideoFile.getAbsolutePath());

        recorder.setVideoFrameRate(camcorderProfile.videoFrameRate);

          try {
              recorder.prepare();
          } 
          catch (IllegalStateException e) {
              Log.e("mytag", "IllegalStateException : " + Log.getStackTraceString(e)); }        
          catch (IOException e) { 
              Log.e("mytag", "IOException : " + Log.getStackTraceString(e)); } 

            recorder.start();
    }

........

........

现在我正在使用适用于Android 5.0的新存储访问框架选择一个文件夹:

    public void FolderSelection_Lollipop() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        startActivityForResult(intent, PICK_FOLDER_CODE_Lollipop);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent resultData) {  
        if(requestCode == PICK_FOLDER_CODE_Lollipop) {
            if(resultCode == RESULT_OK) {
                // Get Uri from Storage Access Framework.
                Uri Uri_Lollipop = resultData.getData();    
                VideoFolderAbsolutePATH = Uri_Lollipop.getPath();
                //also tried:
                VideoFolderAbsolutePATH = Uri_Lollipop.toString();

                // Persist access permissions.
                this.getContentResolver().takePersistableuriPermission(Uri_Lollipop,
                        Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        }
    }

Recorder_Prepare()方法没有用,因为它在Android 5.0上给我IllegalStateException

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐