zoukankan      html  css  js  c++  java
  • 【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();
    }
  • 相关阅读:
    java fx example
    JavaFX 2.0+ WebView /WebEngine render web page to an image
    剑指Offer面试题41(Java版):和为s的两个数字VS和为s的连续正数序列
    时间类(时间戳的各种转换成)
    Android系统各种类型的service刨根解读
    二叉树的建立基本操作(链表方式)(一)
    从头认识java-13.2 利用元组的方式返回多类型对象
    EA初步使用
    inline-block元素设置overflow:hidden属性导致相邻行内元素向下偏移
    下拉框与列表框
  • 原文地址:https://www.cnblogs.com/niray/p/3750066.html
Copyright © 2011-2022 走看看