在我的应用中,我有一个gridview
,它会显示外部存储器中存在的图像。但是有些图像不可见,我在日志中遇到异常
12-08 10:27:02.465 24444-24444/com.example.thiru.gallery D/directory: Camera
12-08 10:27:02.644 24444-24444/com.example.thiru.gallery D/path: /storage/emulated/0/DCIM/Camera/IMG_20171208_073319324_HDR.jpg
12-08 10:27:02.647 24444-24444/com.example.thiru.gallery D/directory: Camera
12-08 10:27:02.818 24444-24444/com.example.thiru.gallery D/path: /storage/emulated/0/DCIM/Camera/IMG_20171208_073319324_HDR.jpg
12-08 10:27:02.819 24444-24444/com.example.thiru.gallery D/directory: 0
12-08 10:27:02.820 24444-24444/com.example.thiru.gallery E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Ringtones: open failed: EISDIR (Is a directory)
12-08 10:27:02.820 24444-24444/com.example.thiru.gallery E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Ringtones: open failed: EISDIR (Is a directory)
但它不会停止或崩溃app
。相反,它在该项目的位置没有显示任何内容。
我从内存中获取图像,
ArrayList<String> imagepath = getAllShownImagesPath(this);
myImageAdapter = new ImageAdapter(this,imagepath);
gridview.setAdapter(myImageAdapter);
我的适配器代码是,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(width/2-10, width/2-10));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setCropToPadding(false);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), width, width);
BitmapDrawable ob = new BitmapDrawable(mContext.getResources(), bm);
imageView.setBackground(ob);
return imageView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
我需要知道在解码路径时是否存在异常,以便从我的列表中删除该图像路径。