保存Bitmap 到存储卡(SD卡) 并通知文件管理器 更新
发布日期:2021-06-29 05:24:59 浏览次数:2 分类:技术文章

本文共 1885 字,大约阅读时间需要 6 分钟。

保存Bitmap 到存储卡(SD卡) 并通知文件管理器 更新

private void saveImage(Bitmap bitmap) {        //此处范围的所谓外部存储是手机的自带内存32G,64G,并不是SD卡,是否有访问权限        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {            File newFileDir = new File(Environment.getExternalStorageDirectory(), "Download");            if (!newFileDir.exists()) {                newFileDir.mkdir();            }            String fileName = System.currentTimeMillis() + ".png";            File file = new File(newFileDir, fileName);            //打开文件输出流            FileOutputStream os = null;            try {                os = new FileOutputStream(file);                bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);                os.flush();                os.close();            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }//            // 保存到图库//            try {//                MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), fileName, null);//            } catch (FileNotFoundException e) {//                e.printStackTrace();//            }////            //通知更新图库//            Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory());//            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);//            sendBroadcast(intent);            MediaScannerConnection.scanFile(this,                    new String[] { file.toString() }, null,                    new MediaScannerConnection.OnScanCompletedListener() {                        public void onScanCompleted(String path, Uri uri) {                            Log.i("ExternalStorage", "Scanned " + path + ":");                            Log.i("ExternalStorage", "-> uri=" + uri);                        }                    });        }    }

转载地址:https://blog.csdn.net/zhf6751134/article/details/99682072 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:go语言学习笔记——外部包函数调用
下一篇:标准转换器,去掉 Retrofit以Mutipart上传参数时,String参数会多一对双引号

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月21日 02时06分27秒