项目场景:(今天写选图和录音部分😁)
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呀 真八错🙉