博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
保存图片到SD卡
阅读量:7109 次
发布时间:2019-06-28

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

/**     * 保存到本地相册     * @param context     * @param bmp     */    public void saveImageToGallery(Context context, Bitmap bmp) {        Log.d("ZoomImage", "saveImageToGallery:" + bmp);        final String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)                ? Environment.getExternalStorageDirectory().getAbsolutePath()                : "/mnt/sdcard";//保存到SD卡        // 首先保存图片        File appDir = new File(SAVE_PIC_PATH + "/ZoomImage/");        if (!appDir.exists()) {            appDir.mkdir();        }        long nowSystemTime = System.currentTimeMillis();        String fileName = nowSystemTime + ".png";        File file = new File(appDir, fileName);        try {            if (!file.exists()) {                file.createNewFile();            }            FileOutputStream fos = new FileOutputStream(file);            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);            fos.flush();            fos.close();        }        catch (FileNotFoundException e) {            e.printStackTrace();        }        catch (IOException e) {            e.printStackTrace();        }        //保存图片后发送广播通知更新数据库        Uri uri = Uri.fromFile(file);        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));//        // 其次把文件插入到系统图库//        try {//            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);//        }//        catch (FileNotFoundException e) {//            e.printStackTrace();//        }//        // 最后通知图库更新//        context.sendBroadcast(//                new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));        Toast.makeText(getContext(), "已保存到本地相册", Toast.LENGTH_LONG).show();    }复制代码

转载地址:http://zavhl.baihongyu.com/

你可能感兴趣的文章
非常正经的看毛片(?)
查看>>
CF 514C(hash)
查看>>
工程代码の初體驗
查看>>
JS节点操作 (表格在js中添加行和单元格,并有一个删除键)
查看>>
poj 1338
查看>>
关于c++中map的内存占用问题
查看>>
SoftEng-logo队成立了!
查看>>
REPLACE...IN.....WITH.... 的使用
查看>>
static变量和static函数
查看>>
linux gcc编译protocol
查看>>
让linux启动后自动进入图形界面或文本界面
查看>>
C# Thread类的应用
查看>>
如何成为资深的python专家
查看>>
在26个大小写字母(52个),以及9个数字组成的字符列表中,随机生成10个8位密码...
查看>>
办公软件word使用技巧 - imsoft.cnblogs
查看>>
swift 学习- 16 -- 构造过程 02
查看>>
Android IOS WebRTC 音视频开发总结(八十三)-- 使用WebRTC广播网络摄像头视频(上)...
查看>>
异步读写(ReadFileEx和ReadFile)之overlapped
查看>>
51nod1582-n叉树
查看>>
Android重绘ListView高度
查看>>