• 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

  • 相关阅读:
    CSUOJ 1554 SG Value
    php面试之四-Linux部分
    php常用算法
    php综合运用技术
    面试基础
    面试题系列1
    面试题系列
    php面试题之五——MySQL数据库(基础部分)
    php面试之数据结构和算法
    asp.net导出Excel类库
  • 原文地址:https://www.cnblogs.com/fishbone-lsy/p/4862484.html
Copyright © 2020-2023  润新知