1. 程式人生 > >Android學習碎片(二)——如何從本地圖片獲得Bitmap例項

Android學習碎片(二)——如何從本地圖片獲得Bitmap例項

準備學習Bitmap相關知識,於是先學習下怎麼從本地圖片獲得Bitmap例項。

第一種方法:

//獲取本地圖片Bitmap例項的第一種方法
        Resources resources = this.getResources();
        InputStream inputStream = resources.openRawResource(R.raw.image1);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(resources, inputStream);
        Bitmap bitmap = bitmapDrawable.getBitmap();

第二種方法:

//獲取本地圖片Bitmap例項的第二種方法
        Resources resources = this.getResources();
        InputStream inputStream = resources.openRawResource(R.raw.image1);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

其實這兩個方法實際上還是一種方法,只不過第一種方法中間經過bitmapDrawable處理後再從其得到bitmap例項,我看了下BitmapDrawable的建構函式原始碼,發現實現上還是用BitmapFactory.DecodeStream方法處理的inputStream,所以兩張方法實際上還是一種,所以直接用下面的辦法就好了。