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

  • 相关阅读:
    EasyExcel无法用转换器或者注解将java字段写入为excel的数值格式
    IE浏览器报400错误:Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    list集合根据字段分组统计转换成map
    博客调网易云歌单JS
    如何一次性add library to classpath
    有趣的统计数据表格显示
    span标签的巧用
    "错误: 找不到或无法加载主类"解决办法
    通过改变注入方式以消除警告
    day17--作业
  • 原文地址:https://www.cnblogs.com/fishbone-lsy/p/4862484.html
Copyright © 2011-2022 走看看