• 【Android】保存Bitmap到SD卡


    1.打开读写SD卡的权限  

    需要在AndroidManifest.xml加入如下代码:

    <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    第一种方法:

    public  void saveBitmap(String bitName, Bitmap mBitmap) {
    File f = new File("/sdcard/" + bitName + ".png");
    try {
    f.createNewFile();
    } catch (IOException e) {
    Tools.ToastShort("在保存图片时出错:" + e.toString());
    }
    FileOutputStream fOut = null;
    try {
    fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
    fOut.flush();
    } catch (IOException e) {
    e.printStackTrace();
    }
    try {
    fOut.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }


    第二种方法:

    1、

    public boolean writePngFile(File outFile) { // 将在屏幕上绘制的图形保存到SD卡
    boolean resault = false; // 存储标识,false为保存失败
    try {
    FileOutputStream fos = new FileOutputStream(outFile); // 创建文件输出流(写文件)
    if (editBitmap[0].compress(Bitmap.CompressFormat.PNG, 100, fos)) { // 将图片对象按PNG格式压缩(质量100%),写入文件
    resault = true; // 存储成功
    }
    fos.flush(); // 刷新
    fos.close();// 关闭流
    } catch (Exception e) {
    e.printStackTrace();
    }
    return resault;
    }

    2、

    public void saveBitmap() {
    final int fileIndex = getSharedPreferences("bitmapIndex",
    Context.MODE_PRIVATE).getInt("index", 0); // 从共享偏好的记录中取出文件流水号,首次从0开始
    new AlertDialog.Builder(this).setTitle("提示信息") // 创建并显示提示对话框
    .setIcon(android.R.drawable.ic_menu_manage) // 设置图标
    .setMessage("保存到SD卡: 钧瓷" + fileIndex + ".png?") // 设置提示信息
    .setPositiveButton("确定", new OnClickListener() { // 按下“确定”按钮的处理
    public void onClick(DialogInterface dialog,
    int which) {
    File outFile = new File(Environment
    .getExternalStorageDirectory()
    .getAbsolutePath()
    + File.separator
    + "钧瓷/钧瓷"
    + fileIndex
    + ".png");// 在SD卡上新建文件
    if (MyCanvas.editArea.writePngFile(outFile)) { // 将绘制路径绘制到位图,并压缩保存
    getSharedPreferences("bitmapIndex",
    Context.MODE_PRIVATE)
    .edit()
    .putInt("index",
    (fileIndex + 1) % 5)
    .commit();// 流水号循环递增0~4
    Tools.ToastShort("保存成功!");
    isSave = false;
    if (index == 2) {
    ScreenNum = 1;
    MyCanvas.menu.creat();
    }
    }
    }
    }).setNegativeButton("取消", new OnClickListener() {// 按下“取消”按钮的处理
    public void onClick(DialogInterface dialog,
    int which) {
    isSave = false;
    if (index == 2) {
    ScreenNum = 1;
    MyCanvas.menu.creat();
    }
    }
    }).create().show();
    }
  • 相关阅读:
    木有晚餐吃的教训暴力图的时候
    HDU1231最大连续子序列DP
    java连连看(GUI有进度条加背景音乐)
    HDU2064简单hanno塔
    HDU1232并查集入门(畅通工程)
    HDU3460Ancient Printer(trie)
    在window下搭建php+apche+masql的方法(个人的蛋疼经历,绝对可靠)
    Java学生管理系统(GUI)(又写了这种破玩意儿了老师,放过我们吧,能不能来点新意)
    VUE使用elpagination添加分页功能
    JS 中深拷贝的几种实现方法
  • 原文地址:https://www.cnblogs.com/niray/p/3750066.html
Copyright © 2020-2023  润新知