自制:图片+音频=视频

项目场景:(今天写选图和录音部分😁)

https://blog.csdn.net/jujujujujuoo/article/details/134693816?spm=1001.2014.3001.5501


首先设置两个按钮,分别用来选择图片和音频。

其次定义一个selectdialog,code如下:

private void openSelectDialog() {

        if (isImage){
            Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            Intent[] intentArray = new Intent[] { photoIntent };
            Intent albumIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            albumIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false); // Whether to allow multiple selections
            Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
            chooserIntent.putExtra(Intent.EXTRA_TITLE, "please choose camera or photos");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
            chooserIntent.putExtra(Intent.EXTRA_INTENT, albumIntent);
            Intent chooser = Intent.createChooser(chooserIntent, "choose photos");
            startActivityForResult(chooser, 1);
        }
        else if (isAudio){
            Intent audioIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
            Intent[] intentArray = new Intent[] { audioIntent };
            Intent recordIntent = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
            Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
            chooserIntent.putExtra(Intent.EXTRA_TITLE, "please choose recorder or recordings");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
            chooserIntent.putExtra(Intent.EXTRA_INTENT, recordIntent);
            Intent chooser = Intent.createChooser(chooserIntent, "choose recorder");
            startActivityForResult(chooser, 2);
        }

    }

作用就是点击按钮后弹出dialog框,通过相机或相册获取图片,通过录音机或录音文件获取音频:


 startActivityForResult回调:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Uri uri = data.getData();
        if (resultCode == RESULT_OK && requestCode == 1) { // Return from the combination selection
            if (data.getData() != null) { // Select a photo from the album
                imagePath = getPathFromUri3(uri);
                Log.i("ffmpeg","imagePath = "+imagePath);
                Toast.makeText(getApplicationContext(), "imagePath = " + imagePath, Toast.LENGTH_SHORT).show();
            } else if (data.getExtras() != null) { // camera
                Bitmap image = (Bitmap) data.getExtras().get("data");
//                Uri imageU = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), image, null,null));

                SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
                Date now = new Date();
                String fileName = "image_" + formatter.format(now) + ".jpg";
                Uri imageU = saveBitmapToUri(image,fileName);

                imagePath = getPathFromUri3(imageU);
                Log.i("ffmpeg","imageU = "+imageU);
                Log.i("ffmpeg","imagePath = "+imagePath);
                Toast.makeText(getApplicationContext(), "imagePath = " + imagePath, Toast.LENGTH_SHORT).show();
                }
                //todo
                //imagePath
        }
        if (resultCode == RESULT_OK && requestCode == 2) { // Return from the combination selection
            if (data.getData() != null) { // Select from the recording file
                audioPath = getPathFromUri3(uri);
                Log.i("ffmpeg","audioPath = "+audioPath);
                Toast.makeText(getApplicationContext(), "audioPath = "+audioPath, Toast.LENGTH_SHORT).show();
            } else if (data.getExtras() != null) { // recorder
                Uri audioU = (Uri) data.getExtras().get("data");
                audioPath = getPathFromUri3(audioU);
                Log.i("ffmpeg","audioPath = "+audioPath);
                //todo
                //audioPath
            }
        }
        
    }

getPathFromUri3方法:

private String getPathFromUri3(Uri uri) {

        String[] projection = {MediaStore.Images.Media.DATA};
        if (isAudio) {
            projection = new String[]{MediaStore.Audio.Media.DATA};
        }
        else if (isVideo){
            projection = new String[]{MediaStore.Video.Media.DATA};
        }
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        assert cursor != null;
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (isAudio){
            column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        }
        else if (isVideo){
            projection = new String[]{MediaStore.Video.Media.DATA};
        }
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        cursor.close();
        return path;
    }

以上可以完成选图和选音频功能,合成视频代码在前篇(使用ffmpeg命令)

hh又完成了一个demo呀 真八错🙉

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值