使用這個類的圖像視圖應用之前給你的位圖縮小到一個較小的所需的大小。
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
class BitmapLoader
{
public static int getScale(int originalWidth,int originalHeight,
final int requiredWidth,final int requiredHeight)
{
//a scale of 1 means the original dimensions
//of the image are maintained
int scale=1;
//calculate scale only if the height or width of
//the image exceeds the required value.
if((originalWidth>requiredWidth) || (originalHeight>requiredHeight))
{
//calculate scale with respect to
//the smaller dimension
if(originalWidth<originalHeight)
scale=Math.round((float)originalWidth/requiredWidth);
else
scale=Math.round((float)originalHeight/requiredHeight);
}
return scale;
}
public static BitmapFactory.Options getOptions(String filePath,
int requiredWidth,int requiredHeight)
{
BitmapFactory.Options options=new BitmapFactory.Options();
//setting inJustDecodeBounds to true
//ensures that we are able to measure
//the dimensions of the image,without
//actually allocating it memory
options.inJustDecodeBounds=true;
//decode the file for measurement
BitmapFactory.decodeFile(filePath,options);
//obtain the inSampleSize for loading a
//scaled down version of the image.
//options.outWidth and options.outHeight
//are the measured dimensions of the
//original image
options.inSampleSize=getScale(options.outWidth,
options.outHeight, requiredWidth, requiredHeight);
//set inJustDecodeBounds to false again
//so that we can now actually allocate the
//bitmap some memory
options.inJustDecodeBounds=false;
return options;
}
public static Bitmap loadBitmap(String filePath,
int requiredWidth,int requiredHeight){
BitmapFactory.Options options= getOptions(filePath,
requiredWidth, requiredHeight);
return BitmapFactory.decodeFile(filePath,options);
}
}
從活動
然後,調用這個類提供從SD卡獲取位圖的文件路徑,並設置requiredWidth和requiredHeight的尺寸
Bitmap reqBitmap = loadBitmap(String filePath,int requiredWidth,int requiredHeight)
方法要縮放位圖。現在使用reqBitmap。
你檢查了這個帖子:http://stackoverflow.com/questions/10271020/bitmap-too-large-to-be-uploaded-into-a-texture? –
這是一個常見錯誤,目前android只支持最高2000px,ü幸運的是不會因爲出錯導致崩潰... –
http://stackoverflow.com/questions/12108841/how-to-catch -error-bitmap-too-large-to-be-upload-into-a-texture – Krishnabhadra