IllegalStateException when recording video to an external SD card with a new storage access platform (SAF)

I have a method below, and it works great for internal storage and an external SD card on Android 4.3 or later:

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

........

........

Now I select the folder using the new storage access platform for 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);
            }
        }
    }

The Recorder_Prepare () method does not work, as it gives me IllegalStateException on Android 5.0+: - (

I also tried following the steps in this link:

Saving a video file using FileDesriptor

The same results ...

The new SAF seems useless to me, although I have already tried to do an online research.

Does anyone know a work or solution for this?

Thank you so much for your help and your good time.

....

Update: 6/8/16 at 10:41

. Recorder_Prepare(), .

+4
1

API- :

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();    
            // Persist access permissions.
            this.getContentResolver().takePersistableUriPermission(Uri_Lollipop,
                    Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
    }
}

......

......

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);
    recorder.setVideoFrameRate(camcorderProfile.videoFrameRate);
    createFile("Video.mp4");
}

private void createFile(String fileName) {
    String mimeType = "video/mp4";
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

    intent.addCategory(Intent.CATEGORY_OPENABLE);

    intent.setType(mimeType);
    intent.putExtra(Intent.EXTRA_TITLE, fileName);
    startActivityForResult(intent, WRITE_REQUEST_CODE 
}

@Override
public void onActivityResult(int requestCode, int resultCode,Intent resultData) {

    if (requestCode == WRITE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        if (resultData != null) {
            URI outputFileUri = resultData.getData();
            FileDescriptor outputFileDescriptor = getContentResolver().openFileDescriptor(outputFileUri, "w").getFileDescriptor()
            recorder.setOutputFile(outputFileDescriptor);

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

, . , . , - . CommonsWare .

+2

All Articles