Store video files on an SD card in Android 5

I am recording video using a class android.media.MediaRecorderthat accepts a path string for the output file ( MediaRecorder.setOutputFile(String)), although there is a version of the method that accepts FileDescriptor.

I need to store huge video files, so I want to use an SD card. To get the path to the corresponding directory, I use Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM). It turns out that the resulting path for the "emulated" storage ( /sdcard/…) is instead of a real SD card ( /sdcard1/on my Xperia Z3 Compact, Android 5.1.1).

I tried to make hardcode /sdcard1/(as well /storage/sdcard1), but it IOExceptionsays permission denied. Of course i have

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

I heard about huge changes in access to the SD card after 4.4 (aka Storage Access Framework), but could not find a simple and clear enough explanation of how to do this in this case. Any help for a concise and concise solution for this?

A PS solution with a hard-coded path would be ok for me, since I am going to use this application only with my phone.

+1
source share
2 answers

This code should solve your problem:

 public String createVideoFilePath() {
        String time = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File[] pathsArray = getExternalFilesDirs(Environment.DIRECTORY_DCIM);

/ pathArray [1] SD . , pathArray [1] pathArray /

        for (File f : pathsArray) {
            if ((f != null) && (Environment.isExternalStorageRemovable(f))) {
                return f.getPath() + File.separator +"video_"+ time + ".mp4";
            }
        }

        return pathsArray[0].getPath() + File.separator +"video_"+ time + ".mp4";

    }

SD- , SD

MediaRecorder.setOutputFile(String) Update: , , , Context. getExternalFilesDirs(String typr) , .

+1

" " Storage Access Framework.

1:

. fileName - , () . . myvideo.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 /* is some random int constant*/);
}

2:

createFile (. 1) , . , , URI . FileDescriptor MediaRecorder.setOutputFile.

@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();
            FileDesriptor outputFileDescriptor = getContentResolver().openFileDescriptor(outputFileUri, "w").getFileDescriptor()
            mMediaRecorder.setOutputFile(outputFileDescriptor);
            mMediaRecorder.start();
        }
    }
}

Activity.

, Save , . pre-KitKat, . "" , . . "ES Explorer" SD- ( ).

+1

All Articles