imageView.setImageBitmap(bitmap)
從res獲得Bitmap:
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.xxx);
從ByteArray獲得Bitmap:
Bitmap bitmap= BitmapFactory.decodeByteArray(data, 0, data.length);
從檔案獲得Bitmap:
Bitmap bitmap= BitmapFactory.decodeFile(file_path);
從BitmapRegionDecoder獲得部分圖片Bitmap:
ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(
file, ParcelFileDescriptor.MODE_READ_ONLY);
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(fileDescriptor.getFileDescriptor(), false);
Bitmap bitmap= decoder.decodeRegion(new Rect(0, 0, decoder.getWidth(), decoder.getHeight()), null);
圖片轉換為Bitmap顯示在ImageView裡面的失真問題:
添加Options屬性
Options opts = new Options();
opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bitmap= BitmapFactory.decodeFile(file_path, opts );
Bitmap bitmap= BitmapFactory.decodeByteArray(data, 0, data.length, opts );