我有问题。请看我的代码。
if(savedInstanceState==null){
mImagePath = getIntent().getStringExtra("path");
new Thread(new Runnable() {
@Override
public void run() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable=true;
ExifInterface exif;
try {
exif = new ExifInterface(new File(mImagePath).getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Bitmap rotatedBitmap = BitmapFactory.decodeFile(mImagePath,options);
if(rotatedBitmap==null){
Answers.getInstance().logCustom(new CustomEvent("Null 1"));
// The first place where sometimes bitmap is null
}
assert rotatedBitmap != null;
int width = rotatedBitmap.getWidth();
int height = rotatedBitmap.getHeight();
if (Math.max(width,height)>MAX_LENGTH_OF_IMAGE){
// if widht or height >1500 px then i resize the image
float factor = MAX_LENGTH_OF_IMAGE/Math.max(width,height);
width=(int)(width*factor);
height=(int)(height*factor);
mOriginalBitmap = Bitmap.createScaledBitmap(rotatedBitmap,width,height,true);
rotatedBitmap.recycle();
} else
mOriginalBitmap=rotatedBitmap;
if(rotate!=0){
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
rotattedBitmap=Bitmap.createBitmap(mOriginalBitmap, 0, 0, width, height, matrix, true);
mOriginalBitmap.recycle();
mOriginalBitmap=rotattedBitmap;
}
if(mOriginalBitmap==null){
Answers.getInstance().logCustom(new CustomEvent("Null 187"));
//I didn't receive this message
}
} catch (IOException e) {
finish();
e.printStackTrace();
}
if(mOriginalBitmap==null){
Answers.getInstance().logCustom(new CustomEvent("Null 206"));
// The second place where bitmap sometimes is null
}
mCurrentBitmap=mOriginalBitmap.copy(Bitmap.Config.ARGB_8888,true);
handler.post(new Runnable() {
@Override
public void run() {
onCreated();
}
});
}
}).start();
}
有时位图为null(在两个地方)。在1000个用户中,有6-7个用户发生此错误。
我始终可以检查位图是否为空,并且不对此位图(bitmap.copy,bitmap.getwidth等)执行任何操作,但是我应该怎么做才能始终加载位图?