zoukankan      html  css  js  c++  java
  • 从网络上获取图片并保存在sdCard上

      1 package com.aib.soft;
      2 
      3 import java.io.BufferedOutputStream;
      4 import java.io.File;
      5 import java.io.FileOutputStream;
      6 import java.io.IOException;
      7 import java.io.InputStream;
      8 import java.net.HttpURLConnection;
      9 import java.net.URL;
     10 import android.graphics.Bitmap;
     11 import android.graphics.BitmapFactory;
     12 import android.util.Log;
     13 
     14 public class Util {
     15 
     16  /*
     17   * 根据url从网络中获取一张图片
     18   * 
     19   */
     20     public static Bitmap getHttpBitmap(String url) {
     21 
     22         URL myFileURL;
     23 
     24         Bitmap bitmap = null;
     25 
     26         try {
     27 
     28             myFileURL = new URL(url);
     29 
     30             // 获得连接
     31 
     32             HttpURLConnection conn = (HttpURLConnection) myFileURL
     33                     .openConnection();
     34 
     35             // 设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制
     36 
     37             conn.setConnectTimeout(60000);
     38 
     39             // 连接设置获得数据流
     40 
     41             conn.setDoInput(true);
     42 
     43             // 不使用缓存
     44 
     45             conn.setUseCaches(false);
     46 
     47             // 这句可有可无,没有影响
     48 
     49             // conn.connect();
     50 
     51             // 得到数据流
     52 
     53             InputStream is = conn.getInputStream();
     54 
     55             // 解析得到图片
     56 
     57             bitmap = BitmapFactory.decodeStream(is);
     58 
     59             // 关闭数据流
     60 
     61             is.close();
     62 
     63         } catch (Exception e) {
     64 
     65             e.printStackTrace();
     66 
     67         }
     68 
     69         return bitmap;
     70 
     71     }
     72 
     73     /**
     74      * Save Bitmap to a file.保存图片到SD卡。
     75      * 
     76      * @param bitmap
     77      * @param file
     78      * @return error message if the saving is failed. null if the saving is
     79      *         successful.
     80      * @throws IOException
     81      */
     82     public static void saveBitmapToFile(Bitmap bitmap, String _file)throws IOException {
     83         BufferedOutputStream os = null;
     84         try {
     85         //     Log.e("aib", "保存图片开始");
     86             File file = new File(_file);
     87             // String _filePath_file.replace(File.separatorChar +
     88             // file.getName(), "");
     89             int end = _file.lastIndexOf(File.separator);
     90             String _filePath = _file.substring(0, end);
     91             File filePath = new File(_filePath);
     92             if (!filePath.exists()) {
     93                 filePath.mkdirs();
     94             }
     95         //    Log.e("aib", "已创建目录");
     96             file.createNewFile();
     97         //    Log.e("aib", "createNewFile()");
     98             os = new BufferedOutputStream(new FileOutputStream(file));
     99          //   Log.e("aib", "new BufferedOutputStream");
    100             bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    101         //    Log.e("aib", "保存图片结束");
    102         } finally {
    103             if (os != null) {
    104                 try {
    105                     os.close();
    106                 } catch (IOException e) {
    107                     Log.e("aib", e.getMessage(), e);
    108                 }
    109             }
    110         }
    111     }
    112 }
  • 相关阅读:
    java-异常体系
    LinkedList (队列、栈实现还没看(打算学会使用方法即可)、另外两篇参考文章还没看)
    java面试题
    javascript function前面的符号!~-
    8.1 Java自学
    7.31 Java自学
    JAVA学习日报 8.1
    JAVA学习日报 7.31
    【C++】类-基础知识
    【机器学习】HMM
  • 原文地址:https://www.cnblogs.com/lolita/p/3410846.html
Copyright © 2011-2022 走看看