MediaRecorder无法写入外部SD卡

时间:2019-04-24 12:54:10

标签: java android mediarecorder

我使用Android Lollipop,尝试使用MediaRecorder在外部SD卡上录制视频,但打开错误失败:EACCES(权限被拒绝)。

我阅读了关于问题How to use the new SD card access API presented for Android 5.0 (Lollipop)?的最受欢迎的答案 它写在那里:

如果要从本机代码访问该Uri,则可以调用ContentResolver.openFileDescriptor(),然后使用ParcelFileDescriptor.getFd()或detachFd()获得传统的POSIX文件描述符整数。

但是我不知道这是否适合我的情况以及如何使用

我从来没有在stackoverflow.com上写过问题,但是这次我很累...


public class MainActivity extends AppCompatActivity {

    private static String card = "sdcard1";  // NOT WORKING
    //private static String card = "sdcard0"; // WORKING
    // PATH TO VIDEO FILE
    public static File Dir = new File("/storage/"+card+"/test");

    static int REQUEST_CODE_STORAGE_ACCESS = 101;

    protected MediaRecorder _rec;
    private Surface _surface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkPermissions();
        @SuppressLint("Recycle")
        SurfaceTexture surfaceTexture = new SurfaceTexture(0);
        _surface = new Surface(surfaceTexture);
        if (!Dir.isDirectory()) if(Dir.mkdirs()) Log.i("MAIN", "Created DIR" + Dir.getAbsolutePath());

        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        intent.putExtra("android.content.extra.SHOW_ADVANCED",true);
        startActivityForResult(intent, REQUEST_CODE_STORAGE_ACCESS);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        if (requestCode == REQUEST_CODE_STORAGE_ACCESS && resultCode == RESULT_OK) {
            Uri treeUri = resultData.getData();
            grantUriPermission(getPackageName(), treeUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            try {
                startRecord();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    void startRecord() throws IOException, RuntimeException {
        String fileName = Dir.getAbsolutePath() + "/" + DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime()) + ".mp4";
        _rec = new MediaRecorder();
        _rec.setAudioSource(MediaRecorder.AudioSource.MIC);
        _rec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
        _rec.setProfile(profile);
        _rec.setOutputFile(fileName);
        _rec.setPreviewDisplay(_surface);
        _rec.prepare();
        _rec.start();
    }

    private void checkPermissions() {
        String[] actions = new String[]{
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.SEND_SMS,
                Manifest.permission.RECEIVE_SMS,
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECEIVE_BOOT_COMPLETED,
                Manifest.permission.INTERNET
        };
        for (String a : actions) {
            checkGrantPermission(a, actions);
        }
    }

    private void checkGrantPermission(String action, String[] actions) {
        int granted = ContextCompat.checkSelfPermission(this, action);
        if (granted != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, actions, 1);
        }
    }

}

0 个答案:

没有答案