Friday, June 17, 2016

How to Convert Drawable to Bitmap in Android




In this tutorial I will explain how to read Drawable from resources as Bitmap and than I will write down some common techniques for processing Bitmaps.


public final static Integer[] imageResIds = new Integer[]{
R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e, R.drawable.f,
R.drawable.k, R.drawable.l, R.drawable.m};


loadBitmap(imageResIds[index], mImageView);



public void loadBitmap(int resId, ImageView imageView) {
 //Now we have bitmap object from drawable resource. we can scale this bitmap
 //according to device screen density.
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), data);

}

Note: This method can cause out of memory error.
- If you want to avoid this exception use scaled down version of bitmap in memory.
- Process Bitmaps Off the UI thread (Use Async Task).




Author:
Hammad Tariq
Android Developer




No comments:

Post a Comment

Kotlin Android MVP Dagger 2 Retrofit Tutorial

http://developine.com/building-android-mvp-app-in-kotlin-using-dagger-retrofit/