NullPointerException从Android文件重新缩放位图
一个月以来,我一直面临着这个问题,没有解决方案: 我需要将一个文件转换成位图并重新缩放它。 我使用以下方法,摘自android指南:NullPointerException从Android文件重新缩放位图,android,bitmap,nullpointerexception,out-of-memory,bitmapfactory,Android,Bitmap,Nullpointerexception,Out Of Memory,Bitmapfactory,一个月以来,我一直面临着这个问题,没有解决方案: 我需要将一个文件转换成位图并重新缩放它。 我使用以下方法,摘自android指南: 公共位图解码SampledBitMapFromFile(字符串路径,int-reqSize){ //使用INJUSTDECBOUNDS首次解码=true检查尺寸 BitmapFactory.Options=new-BitmapFactory.Options(); options.inJustDecodeBounds=true; options.inMutable=
公共位图解码SampledBitMapFromFile(字符串路径,int-reqSize){
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=true;
options.inMutable=true;
解码文件(路径、选项);
//计算样本大小
双重比率=(双重)options.outHeight/options.outWidth;
布尔纵向=真;
如果(比率<1){
肖像=假;
}
double floatSampleSize=options.outHeight/(double)reqSize;
options.inSampleSize=(int)Math.floor(floatSampleSize);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
位图结果位图=位图工厂.decodeFile(路径,选项);
if(floatSampleSize%options.insSampleSize!=0){
如果(肖像){
resultBitmap=Bitmap.createScaledBitmap(resultBitmap,(int)(reqSize/比率),reqSize,true);
}否则{
resultBitmap=Bitmap.createScaledBitmap(resultBitmap,reqSize,(int)(比率*reqSize),true);
}
}
返回结果位图;
}
我有相同的NullPointerException,但无法复制它:
NullPointerException(@Utilities:decodeSampledBitmapFromFile:397){AsyncTask#2}NullPointerException(@Utilities:decodeSampledBitmapFromFile:399){AsyncTask#3} 第397行和第399行是: resultBitmap=Bitmap.createScaledBitmap(resultBitmap,(int)(reqSize/比率),reqSize,true)
resultBitmap=Bitmap.createScaledBitmap(resultBitmap,reqSize,(int)(比率*reqSize),true) 在此设备型号中: ST21a
U9500
M7
U8950-1 GT-I9305 有人能帮我解决这个问题吗? BitmapFactory.decodeFile是否返回空值?
非常感谢试试这个希望对你有用
public Bitmap decodeFile(String path, int reqSize) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = reqSize;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
大多数情况下,给定的路径可能是错误的。或者,如果您正在解码一个目录中的多个图像,则可能有一个文件夹正试图将其解码为图像。确保您没有尝试将文件夹解码为图像,并且图像路径有效。使用log语句查看您试图解码的内容。谢谢Torcellite,但我只是在调用decodeSampledBitmapFromFile之前检查字符串路径是否是有效的图像文件。不管怎样,我会追踪日志。
public Bitmap decodeFile(String path, int reqSize) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = reqSize;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}