嘗試使用此代碼從文件加載圖像:
img.setImageBitmap(decodeSampledBitmapFromFile(imagePath, 1000, 700));
時decodeSampledBitmapFromFile
:
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
{ // BEST QUALITY MATCH
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight) {
inSampleSize = Math.round((float)height/(float)reqHeight);
}
int expectedWidth = width/inSampleSize;
if (expectedWidth > reqWidth) {
//if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width/(float)reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
您可以使用數字(在這種情況下爲1000,700)來配置圖像文件輸出的質量。
非常感謝,我會試試看,還有什麼是推薦的參數?那圖像不會那麼大,而且還保持良好的品質? – 2013-04-09 22:24:01
取決於你想應用這個圖像的ImageView的大小... – 2013-04-09 22:24:59
如果我想要的圖像,直到0.2MByte,我也可以得到一個很好的決議?也許你知道其他方式,而不使用位圖?或使用方式從SD卡加載圖像而不加載到我的應用程序數據空間? – 2013-04-09 22:28:34