In my app, I have a gridview
which will display images exists in external memory. But some images are not visible and I am getting exception in my log as
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)
But it doesn't stop or crash the app
. Instead it shows nothing in the place of that item.
I am getting the images from memory as,
ArrayList<String> imagepath = getAllShownImagesPath(this);
myImageAdapter = new ImageAdapter(this,imagepath);
gridview.setAdapter(myImageAdapter);
My adapter code is,
@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;
}
I need to know if there is exception while decoding the path to remove that image path from my list.