Android调用系统照相机拍摄视频并将其拷贝到制定的文件夹下

通过调用系统照相机拍摄视频并将其复制到指定的文件夹下。

 Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

//0为匹配的标示符,和返回的requestCode对应。
 startActivityForResult(intent, 0);



@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        Log.i(RecordActivity.this.toString(), "录制结果" + "requestCode:" + requestCode + ", " +
                "" + "resultCode:" + resultCode + ", data:" + intent.toString() + intent.getData().toString());

        //如果返回的request代码和startActivityForResult的代码不匹配或录制取消则直接返回
        if(requestCode != 0){
            return ;
        }else if(resultCode != -1){
            return;
        }

        //通过Uri获取文件的绝对地址
        Uri uri = intent.getData();
        Cursor cursor = this.getContentResolver().query(uri, null, null,
                null, null);
        String filePath = null;
        if (cursor != null && cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.VideoColumns._ID));
            filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA));

            cursor.close();
        }

        //将文件拷贝到制定的文件夹下
        if(null != filePath){
            File tmpFile = new File(filePath);
            String fileSuffix = filePath.substring(filePath.lastIndexOf("."));
            if(tmpFile.exists()){
                String localPath = UserAccount.getInstance().getLocalDir("video/")+nameEdit.getText()+fileSuffix;
                File localFile = new File(localPath);
                tmpFile.renameTo(localFile);
                tmpFile.delete();
                Log.i(RecordActivity.this.toString(), "文件地址"+filePath+localFile.getAbsolutePath());
            }
        }
    }



你可能感兴趣的:(Android调用系统照相机拍摄视频并将其拷贝到制定的文件夹下)