0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
  • Possible duplicate of [How do I check if a file exists in Java?](https://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java) – ADM Dec 08 '17 at 05:36
  • @ADM But I am getting the images by `imagepath = getAllShownImagesPath(this);` Then why should I check whether it exists or not. – Jyoti JK Dec 08 '17 at 05:41
  • 1
    You did not post the code for that function. Your excuse is not to the point. Moreover you could post full path. Further its unclear how you use the result of that function. Is it only one path? Or an array? You are not very informative. – greenapps Dec 08 '17 at 08:50
  • @greenapps I edited my question. please refer it – Jyoti JK Dec 08 '17 at 09:39
  • You did not give all the info i asked for. Please post more lines from the log too. – greenapps Dec 08 '17 at 09:56

0 Answers0