Here you can find the source of getBitmap(String url, Context context, String newPicName)
public static boolean getBitmap(String url, Context context, String newPicName)
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import com.busx.protocol.ProtocolDef; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.util.Log; public class Main{ private static final String TAG = "BtimapUtil"; public static boolean getBitmap(String url, Context context, String newPicName) {// ww w.ja va2 s .c o m Log.e(TAG, "url=" + url); String imageName = url.substring(url.lastIndexOf("/") + 1, url.length()); File file = new File(Utils.getPath(ProtocolDef.SDPACKAGEIMAGE, ProtocolDef.INPACKAGEIMAGE), imageName); File file2 = new File(Utils.getPath(ProtocolDef.SDPACKAGEIMAGE, ProtocolDef.INPACKAGEIMAGE) + newPicName); return getNetBitmap(url, file, context, file2); } public static boolean getNetBitmap(String strUrl, File file, Context context, File file2) { Log.e(TAG, "getBitmap from net"); Bitmap bitmap = null; InputStream in = null; FileOutputStream out = null; try { URL url = new URL(strUrl); HttpURLConnection con = (HttpURLConnection) url .openConnection(); con.setDoInput(true); con.connect(); in = con.getInputStream(); bitmap = BitmapFactory.decodeStream(in); out = new FileOutputStream(file.getPath()); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); file.renameTo(file2); out.flush(); } catch (MalformedURLException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (null != out) { out.close(); } if (null != in) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; } }