Android Bitmap Load getBitmap(String url, Context context, String newPicName)

Here you can find the source of getBitmap(String url, Context context, String newPicName)

Description

get Bitmap

Declaration

public static boolean getBitmap(String url, Context context,
            String newPicName) 

Method Source Code

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;
    }
}

Related

  1. decodeSampledBitmapStreamForSize(InputStream is, int reqWidth, int reqHeight)
  2. download(String url, String fileName)
  3. fetchImage(final Context context, final String url, final BitmapFactory.Options decodeOptions, final Object cookie, final OnFetchCompleteListener callback)
  4. fetchImage(final Context context, final String url, final OnFetchCompleteListener callback)
  5. getBitmap(Resources resources, int resourceId)
  6. getBitmapFromAsset(Context context, String strName)
  7. getBitmapFromFile(File file)
  8. getBitmapFromFile(String filePath)
  9. getBitmapFromFileInputStream(FileInputStream is)