Android 调用原生系统的拍照,录音,录像并保存为文件

声明requestCode常量:

public strait final int TACK_RECORD =10000;
public strait final int TACK_PHOTO =10001;
public strait final int TACK_VIDEO =10002;

1.拍照代码:

Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    openCameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
    tempFile = new File(FilePath.RootPath + "/" + FilePath.Temp + "/" +System.currentTimeMillis()+".jpg");
    Uri filePUri = FileProvider.getUriForFile(getImplContext(), FilePath.FileProvider, tempFile);
    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, filePUri); //指定图片存放位置,指定后,在onActivityResult里得到的Data将为null
    startActivityForResult(openCameraIntent, ARKey.TACK_PHOTO); // 参数常量为自定义的request code, 在取返回结果时有用

2.录像代码:

 Intent openVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    openVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
    tempFile = new File(user.getUserFileSubPath() + FilePath.Video + "/" +System.currentTimeMillis()+".mp4");
    Uri fileVUri = FileProvider.getUriForFile(getImplContext(), FilePath.FileProvider, tempFile);
    openVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileVUri);
    startActivityForResult(openVideoIntent, ARKey.TACK_VIDEO); // 参数常量为自定义的request code, 在取返回结果时有用

3.录音代码:

 Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
    startActivityForResult(intent, ARKey.TACK_RECORD);

调用原生系统之后在onactivityForResult中返回结果代码如下:

@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == Activity.RESULT_OK){
        switch (requestCode){
            case TACK_PHOTO:
                savePhone(tempFile);
                break;

            case TACK_VIDEO:
                saveVideo(tempFile);
                break;

            case TACK_RECORD:
                saveRecord(data);
                break;
        }
    }
}

调用保存照片,保存录像,保存录音的方法,保存路径需自己更改为自己的路径

1.保存照片

private void savePhone(File file){
    Observable.just(file)
            .observeOn(Schedulers.io())
            .compose(HomeFragment.this.bindUntilEvent(FragmentEvent.DESTROY))
            .map(new Function() {
                @Override
                public File apply(File rawFile) throws Exception {
                    File compressFile = Luban.with(getImplContext()).get(rawFile.getPath());
                    FileUtil.delete(rawFile);

                    return compressFile;
                }
            })
            .map(new Function() {
                @Override
                public File apply(File rawFile) throws Exception {
                    File newFile = FileUtil.copySdcardFile(rawFile.getPath(), user.getUserFileSubPath() + FilePath.Photo + "/" +rawFile.getName());
                    FileUtil.delete(rawFile);
                    return newFile;
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(File file) {
                    InputFileNamePopup popup = new InputFileNamePopup(getImplContext());
                    popup.setFile(file);
                    popup.showAtLocation(mContentView, 0, 0, Gravity.CENTER);
                }

                @Override
                public void onError(Throwable e) {
                    LogUtils.e(e);

                }

                @Override
                public void onComplete() {
                }
            });

}

2.保存录像:

private void saveVideo(File file){
    InputFileNamePopup popup = new InputFileNamePopup(getImplContext());
    popup.setFile(file);
    popup.showAtLocation(mContentView, 0, 0, Gravity.CENTER);
}

3.保存录音:

private void saveRecord(Intent data){
    Observable.just(data)
            .observeOn(Schedulers.io())
            .compose(HomeFragment.this.bindUntilEvent(FragmentEvent.DESTROY))
            .map(new Function() {
                @Override
                public String apply(Intent intent) throws Exception {
                    Uri uri = intent.getData();
                    String filePath = FileUtil.getAudioFilePathFromUri(getActivity(), uri);
                    LogUtils.d(filePath);
                    return filePath;
                }
            })
            .map(new Function() {
                @Override
                public File apply(String s) throws Exception {
                    File file = new File(s);
                    return FileUtil.copySdcardFile(s, user.getUserFileSubPath() + FilePath.Record + "/" + file.getName());
                }
            })

            .observeOn(AndroidSchedulers.mainThread())

            .subscribe(new Observer() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(File f) {
                    InputFileNamePopup popup = new InputFileNamePopup(getImplContext());
                    popup.setFile(f);
                    popup.showAtLocation(mContentView, 0, 0, Gravity.CENTER);
                }

                @Override
                public void onError(Throwable e) {
                    LogUtils.e(e);
                }

                @Override
                public void onComplete() {

                }
            });

}

 

你可能感兴趣的:(Android)