Android Utililty Methods Bitmap Load

List of utility methods to do Bitmap Load

Description

The list of methods to do Bitmap Load are organized into topic(s).

Method

BitmapdecodeSampledBitmapFromUrl(URL url, int reqWidth, int reqHeight)
decode Sampled Bitmap From Url
URLConnection uc = url.openConnection();
InputStream is = new BufferedInputStream(uc.getInputStream());
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
is.close();
options.inSampleSize = calculateInSampleSize(options, reqWidth,
        reqHeight);
...
BitmapdecodeSampledBitmapStreamForSize(InputStream is, int reqWidth, int reqHeight)
decode Sampled Bitmap Stream For Size
is.mark(is.available());
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
        reqHeight);
options.inJustDecodeBounds = false;
is.reset();
...
voiddownload(String url, String fileName)
Downloads file from specified URL and stores as filename mentioned as parameter, in default external storage directory.
Bitmap bmp = getFromUrl(url);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 0, bytes);
File f = new File(Environment.getExternalStorageDirectory()
        + File.separator + fileName);
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
...
voidfetchImage(final Context context, final String url, final BitmapFactory.Options decodeOptions, final Object cookie, final OnFetchCompleteListener callback)
Only call this method from the main (UI) thread.
new AsyncTask<String, Void, Bitmap>() {
    @Override
    protected Bitmap doInBackground(String... params) {
        final String url = params[0];
        if (TextUtils.isEmpty(url)) {
            return null;
        File cacheFile = null;
...
voidfetchImage(final Context context, final String url, final OnFetchCompleteListener callback)
Only call this method from the main (UI) thread.
fetchImage(context, url, null, null, callback);
BitmapgetBitmap(Resources resources, int resourceId)
Utility method to get bitmap from cache or, if not there, load it from its resource.
Bitmap bitmap = sBitmapResourceMap.get(resourceId);
if (bitmap == null) {
    bitmap = BitmapFactory.decodeResource(resources, resourceId);
    sBitmapResourceMap.put(resourceId, bitmap);
return bitmap;
booleangetBitmap(String url, Context context, String newPicName)
get Bitmap
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);
...
BitmapgetBitmapFromAsset(Context context, String strName)
get Bitmap From Asset
AssetManager assetManager = context.getAssets();
InputStream istr;
Bitmap bitmap = null;
try {
    istr = assetManager.open(strName);
    bitmap = BitmapFactory.decodeStream(istr);
} catch (IOException e) {
    return null;
...
BitmapgetBitmapFromFile(File file)
get Bitmap From File
FileInputStream fis;
try {
    fis = new FileInputStream(file);
} catch (Exception e) {
    FrameworkLog.e(TAG,
            "getBitmapFromFile Exception: " + e.getMessage());
    return null;
return getBitmapFromFileInputStream(fis);
BitmapgetBitmapFromFile(String filePath)
get Bitmap From File
return getBitmapFromFile(new File(filePath));