Android Bitmap Load getNetBitmap(String strUrl, File file, Context context, File file2)

Here you can find the source of getNetBitmap(String strUrl, File file, Context context, File file2)

Description

get Net Bitmap

Declaration

public static boolean getNetBitmap(String strUrl, File file,
            Context context, File file2) 

Method Source Code

//package com.java2s;
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 android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.util.Log;

public class Main {
    private static final String TAG = "BtimapUtil";

    public static boolean getNetBitmap(String strUrl, File file,
            Context context, File file2) {
        Log.e(TAG, "getBitmap from net");
        Bitmap bitmap = null;/* www  . j a va 2  s. c o m*/
        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;
    }
}

Related

  1. getBitmapFromFile(String filePath)
  2. getBitmapFromFileInputStream(FileInputStream is)
  3. getBitmapFromRes(Context context, int resId)
  4. getFromUrl(String url)
  5. getImageFromUri(Context ctx, Uri uri, int reqWidth, int reqHeight)
  6. loadBitmap(Activity activity, int resId, ImageView imageView, int reqWidth, int reqHeight)
  7. loadBitmap(Context c, String fileName)
  8. loadBitmap(Context c, String fileName, int width)
  9. loadBitmap(Context c, String fileName, int width, boolean sample)