android调用保存相册方法MediaStore.Images.Media.insertImage,部分机型返回null

在使用三星手机时遇到MediaStore.Images.Media.insertImage返回null,导致保存图片失败。问题根源在于缺少WRITE_EXTERNAL_STORAGE权限。通过检查并申请该权限,可以解决部分手机无法保存图片的问题。确保在Android 6.0及以上版本同时请求READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE权限,避免因权限不足引发的保存错误。

在调试的时候,使用的模拟器和华为手机均正常保存。用三星的手机时,MediaStore.Images.Media.insertImage返回值为null,保存失败。

解决方案就是:检查权限是否包含了:READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE。由于我写的时候只申请了READ_EXTERNAL_STORAGE权限,没有申请WRITE_EXTERNAL_STORAGE权限,导致部分手机保存失败。

public boolean checkPermissionREAD_EXTERNAL_STORAGE() {
        int currentAPIVersion = Build.VERSION.SDK_INT;
        if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(app,Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                    ContextCompat.checkSelfPermission(app,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,Manifest.permission.READ_EXTERNAL_STORAGE) ||
                        ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {    //点击了不再提示
                    this.showAlertDialog("保存失败,请在<设置/权限管理>中开启存储权限后重试!");
                } else {
                    ActivityCompat.requestPermissions(app,new String[] { Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE },MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
                }
                return false;
            } else {
                //成功
                return true;
            }
        } else {
            //成功
            return true;
        }
    }

 

public class VideoUtils { public static void saveVideoToGallery(Context context, File videoFile) { // 创建保存视频的目标路径 File destFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), videoFile.getName()); try { // 复制视频文件到目标路径 copyFile(videoFile, destFile); // 更新媒体库,使其扫描并添加到相册 updateMediaStore(context, destFile); } catch (IOException e) { e.printStackTrace(); } } private static void copyFile(File sourceFile, File destFile) throws IOException { FileChannel sourceChannel = null; FileChannel destChannel = null; try { sourceChannel = new FileInputStream(sourceFile).getChannel(); destChannel = new FileOutputStream(destFile).getChannel(); destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); } finally { if (sourceChannel != null) { sourceChannel.close(); } if (destChannel != null) { destChannel.close(); } } } private static void updateMediaStore(Context context, File videoFile) { ContentValues values = new ContentValues(); values.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath()); values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4"); values.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000); Uri uri = context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values); // 通知媒体库刷新 MediaScannerConnection.scanFile(context, new String[]{videoFile.getAbsolutePath()}, null, null); } }解释这段代码,这段代码是否是插入数据库的方式存储视频数据
最新发布
03-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值