安卓获取手机视频和图片
获取手机本地视频
首先需要获得手机内存权限
在AndroidManifest.xml中添加代码,获得读取权限
android.permission.READ_EXTERNAL_STORAGE
但是后来我发现android采用了Runtime permission request, 即在运行时请求权限,而不是在编译的时候,即使在manifest.xml里面添加了权限也要在运行时请求
这时可以在运行之后做一下权限判断,如果没有权限则弹出对话框,向用户申请权限,这段代码我写在了刚运行app的第一个activity的oncreate里面,且只需要做一次判断,之后再进入app不需要再开启权限
public static void verifyStoragePermissions(Activity activity) {
try {
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
定义方法chooseVideo,为某个按钮设置监听器,当点击按钮,执行方法chooseVideo,在方法中首先做机型适配:
private void chooseVideo(View view) {
flag=0;
if(android.os.Build.BRAND.equals("Huawei")){
Intent intentPic = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
this.startActivityForResult(intentPic,2);
}
if(android.os.Build.BRAND.equals("Xiaomi")){
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "video/*");
startActivityForResult(Intent.createChooser(intent, "选择要导入的视频"), 2);
}else {
Intent intent = new Intent();
if(Build.VERSION.SDK_INT < 19){
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
}else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("video/*");
}
startActivityForResult(Intent.createChooser(intent, "选择要导入的视频"), 2);
}
}
再重写方法onActivityResult,里面实现的功能有查询文件路径,并且实现函数回调
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
String[] proj = {MediaStore.Images.Media.DATA};
ContentResolver resolver = getContext().getContentResolver();
Cursor cursor = resolver.query(uri, proj, null, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
File file = new File(path);
}
然后获得视频的第一帧作为视频封面呈现在可视化界面上,大概思路是先将图片存储,然后根据图片地址,为页面设置背景图片,就可以达到选择某段视频后,以第一帧作为封面的效果
String videoName = path.split("/")[6].split("\\.")[0];
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(file.getAbsolutePath());
bitmap = mmr.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(new File(this.getContext().getExternalCacheDir().getAbsolutePath() + "/" + videoName + ".jpg"));
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, outStream);
String imagePath = this.getContext().getExternalCacheDir().getAbsolutePath() + "/" + videoName + ".jpg";
File image = new File(imagePath);
Uri imUri = Uri.fromFile(image);
bitmap = BitmapFactory.decodeStream(getContext().getContentResolver().openInputStream(imUri));
imageView.setImageBitmap(bitmap);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
(上面的代码看上去有点繁琐,有人问为什么不能在获得视频的第一帧的Bitmap对象之后就设置imageView的Bitmap,我试过了但是会设置失败,图片不显示,所以用了一个比较麻烦的方法,如果有更简单的方法麻烦告诉我一下:)
获取手机本地图片
获取手机图片的方法和获取视频的方法大同小异,首先监听器加上一个方法,不要忘记机型适配
private void chooseSurpass() {
Log.d("TAG", "chooseSurpass: -----选中");
flag=1;
if(android.os.Build.BRAND.equals("Huawei")){
Intent intentPic = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
this.startActivityForResult(intentPic,2);
}
if(android.os.Build.BRAND.equals("Xiaomi")){
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(Intent.createChooser(intent, "选择要导入的图片"), 2);
}else {
Intent intent = new Intent();
if(Build.VERSION.SDK_INT < 19){
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
}else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
}
startActivityForResult(Intent.createChooser(intent, "选择要导入的图片"), 2);
}
}
再重写方法onActivityResult,里面实现的功能有查询文件路径,并且实现函数回调
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
String[] proj = {MediaStore.Images.Media.DATA};
ContentResolver resolver = getContext().getContentResolver();
Cursor cursor = resolver.query(uri, proj, null, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
Log.d("TAG", "onActivityResult: -------"+path);
File file = new File(path);
surpass.setImageURI(uri);
}
效果
|