zoukankan      html  css  js  c++  java
  • Android中图片的文件储存

      两种常见情况。1、储存一个bitmap,2、直接下载一个图片并储存。

      1、将一个bitmap存成文件

    public static void saveMyBitmap(Bitmap mBitmap, String fileName) {
            // 新建文件
            File f = new File(fileName);
            // 新建文件输出流
            FileOutputStream fOut = null;
            try {
                fOut = new FileOutputStream(f);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 将bitmap压缩至文件输出流
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            try {
                fOut.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

      其中,核心的方法就是Bitmap类中的compress方法。

    public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) {
            throw new RuntimeException("Stub!");
        }

      这个方法有三个参数,第一个参数是压缩格式,第二个是压缩质量(最大是100),第三个是文件输出流。

    public static enum CompressFormat {
            JPEG,
            PNG,
            WEBP;
    
            private CompressFormat() {
            }
        }

      由上面代码易知,图片可以选择三种格式压缩。

      2、直接下载一个图片并储存

      

    public class DownloadTask extends AsyncTask<String, Integer , String> {
    
        Context context;
    
        public DownloadTask(Context context){
            this.context = context;
        }
    
        @Override
        protected String doInBackground(String... strings) {
            // 文件的下载地址
            String fileUrl = strings[0];
            // 文件名
            String fileName = strings[1];
            // 文件的扩展名
            String expandName = strings[2];
    
            try {
                // 下载图片
                URL u = new URL(fileUrl);
                InputStream is = u.openStream();
                DataInputStream dis = new DataInputStream(is);
                // 保存图片文件
                byte[] buffer = new byte[1024];
                int length;
                FileOutputStream fos = new FileOutputStream(new File(fileName + expandName));
                while ((length = dis.read(buffer))>0) {
                    fos.write(buffer, 0, length);
                }
                // 扫描指定文件使媒体更新
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri uri = Uri.fromFile(new File(fileName + expandName));
                intent.setData(uri);
                context.sendBroadcast(intent);
    
            } catch (MalformedURLException mue) {
                mue.printStackTrace();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } catch (SecurityException se) {
                se.printStackTrace();
            }
    
            return (fileName+expandName);
        }
    
        @Override
        protected void onPostExecute(String fileName) {
            UIUtil.toastMessage(context, "下载成功,图片已保存至:" + fileName);
    
        }

      Done

  • 相关阅读:
    swift 第十四课 可视化view: @IBDesignable 、@IBInspectable
    swift 第十三课 GCD 的介绍和使用
    swift 第十二课 as 的使用方法
    swift 第十一课 结构体定义model类
    swift 第十课 cocopod 网络请求 Alamofire
    swift 第九课 用tableview 做一个下拉菜单Menu
    swift 第八课 CollectView的 添加 footerView 、headerView
    swift 第七课 xib 约束的优先级
    swift 第六课 scrollview xib 的使用
    swift 第五课 定义model类 和 导航栏隐藏返回标题
  • 原文地址:https://www.cnblogs.com/fishbone-lsy/p/4862484.html
Copyright © 2011-2022 走看看