Android 10.0获取本地视频及其预览图

打开本地视频选择器

private void openLocalVideo(){
    
	Intent intent = new Intent();
	intent.setAction(Intent.ACTION_GET_CONTENT);
   intent.setDataAndType(android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI,"video/*");
        getActivity().startActivityForResult(intent,IntentCode.REQUEST_VIDEO_CAPTURE);
}

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("YM","本地视频获取的数据:requestCode->"+requestCode+"-->resultCode:"+resultCode);
        if (requestCode == IntentCode.REQUEST_VIDEO_CAPTURE){
            Uri videoUri = data.getData();
        }
    }
注意:
1、该代码在华为10.0上面打开后页面会包含图片和视频
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
getActivity().startActivityForResult(intent,IntentCode.REQUEST_VIDEO_CAPTURE);
2、关于打开本地视频的其余注意事项,参考以下链接:

Android 跳转系统选择本地视频的功能:https://www.cnblogs.com/widgetbox/p/7503894.html

读取本地视频预览图

由于Android10.0的存储方式进行了改变,大体上分为私有存储和共享文件。当我们使用Cursor来进行查询本地视频资源的时候只能查询到共享文件中的视频,同时也可以查询到该视频的预览图,但是上述打开本地视频选择器的功能却可以显示私有存储里面的文件,所以用上述打开本地视频选择器返回的Uri通过cursor去查询本地视频时候是查不到的,所以预览图也是没有的。针对此种问题需要通过以下方式来获取视频并生成预览图操作:

MediaExtraBean.java//这里面为了行文方便没有编写set和get方法,所以下文中进行测试时候需要注意
public class MediaExtraBean {
    public Uri thumbPath;//缩略图路径
    public String localPath = "";//路径
    public String title  = "";//视频名称
    public int duration;//视频时长
    public long size;//视频大小
    public String imagePath;//视频缩略图路径
}

public class MediaUtils {

    public MediaExtraBean getRingDuring(Uri mUri){
        Log.e("YM","文件路径:"+mUri);
        String duration=null;
        MediaExtraBean mediaExtraBean = new MediaExtraBean();
        android.media.MediaMetadataRetriever mmr = new android.media.MediaMetadataRetriever();

        try {
            ParcelFileDescriptor parcelFileDescriptor =
                    ImProvider.context.getContentResolver().openFileDescriptor(mUri, "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            HashMap<String, String> headers=null;
            headers = new HashMap<String, String>();
            headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; MW-KW-001 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/1.0.0.001 U4/0.8.0 Mobile Safari/533.1");
//                mmr.setDataSource(mUri, headers);
            mmr.setDataSource(fileDescriptor);
            duration = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);
            mediaExtraBean.setDuration(Integer.parseInt(duration));
            Log.e("YM","时长:"+duration);
            Bitmap bitmap = mmr.getFrameAtTime();//获取第一帧,然后保存到本地
// Load thumbnail of a specific media item.
//            Bitmap thumbnail =
//                    ImProvider.context.getApplicationContext().getContentResolver().loadThumbnail(
//                            mUri, new Size(640, 480), null);
            Uri uri = createImageFile();
            ImProvider.context.grantUriPermission(ImProvider.context.getPackageName(), uri,
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            try {
                ParcelFileDescriptor imageFd = ImProvider.context.getContentResolver().openFileDescriptor(uri, "w");
                FileDescriptor thumb = imageFd.getFileDescriptor();
                FileOutputStream fileOutputStream = new FileOutputStream(thumb);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
                mediaExtraBean.setThumbPath(uri);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            mmr.release();
        }
        return mediaExtraBean;
    }

    private Uri createImageFile(){
        Uri uri;
        String timeStamp =
                new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        String imageFileName = String.format("JPEG_%s.png", timeStamp);
        String status = Environment.getExternalStorageState();
        // 判断是否有SD卡,优先使用SD卡存储,当没有SD卡时使用手机存储
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME,imageFileName);
        contentValues.put(MediaStore.Audio.Media.MIME_TYPE, "image/png");//修改文件类型,默认为jpg
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            uri = ImProvider.context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        } else {
            uri = ImProvider.context.getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, contentValues);
        }
        Log.e("YM","文件路径:"+uri.toString());
        return uri;
    }

}

你可能感兴趣的:(Android,日常bug)