Kaitis
Kaitis

Reputation: 638

FileNotFoundException using Universal Image Loader

This is my first app on Android and I am using the Universal Image Loader Library to load images into a GridView.

The problem I am having is

FileNotFoundException: No entry for content://media/external/images/thumbnails/...(all thumbnails )

The thumbnails are not loaded in the GridView, but when I tap on a gridItem, the Intent is started and an imageId is passed to the next activity.

Here is the code I am using (based on the Sample example at UIL( github)

public class ImageGridFragment extends AbsListViewBaseFragment {


    Cursor mCursor;
    int[]imageIDs;
    String bucket;
    ArrayList<String> imageUrls;
    int firstImageIndex;
    int lastImageIndex;
    int columnIndex;
    String sender;

    DisplayImageOptions options;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getActivity().getIntent();
        sender = intent.getExtras().getString("sender");
        bucket = intent.getExtras().getString("albumName");
        imageIDs = getImagesFromBucket();
        imageUrls = new ArrayList<>();

        for(int i = 0; i < imageIDs.length; i++){
            int imageID = imageIDs[i];
            Uri imageURI = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID);
            imageUrls.add(imageURI.toString());
        }

        BitmapFactory.Options resizeOptions = new BitmapFactory.Options();
        resizeOptions.inSampleSize = 3; // decrease size 3 times
        resizeOptions.inScaled = true;

        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .decodingOptions(resizeOptions)
                .postProcessor(new BitmapProcessor() {
                    @Override
                    public Bitmap process(Bitmap bmp) {
                        return Bitmap.createScaledBitmap(bmp, 120, 120, false);
                    }
                })
                .build();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_gallery_grid_view, container, false);
        listView = (GridView) rootView.findViewById(R.id.gridView);
        listView.setAdapter(new ImageAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent returnToSender;
                if (sender.equals("First")) {
                    returnToSender = new Intent(ImageGridFragment.this.getActivity(), FirstImageActivity.class);

                    firstImageIndex = 0;
                    returnToSender.putExtra("firstImageIndex", firstImageIndex);

                    // Move cursor to current position
                    mCursor.moveToPosition(position);
                    // Get the current value for the requested column
                    int imageID = mCursor.getInt(columnIndex);
                    Uri imageURI = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + imageID);
                    returnToSender.putExtra("imageURI", imageURI);

                    String comingFrom = "gridView";
                    returnToSender.putExtra("comingFrom", comingFrom);
                    mCursor.close();

                    returnToSender.putExtra("albumName", bucket);
                } else {
                    returnToSender = new Intent(ImageGridFragment.this.getActivity(), LastImageActivity.class);
                    lastImageIndex = 0;
                    returnToSender.putExtra("lastImageIndex", lastImageIndex);
                }
                startActivity(returnToSender);

            }
        });

            return rootView;
    }

    private int[] getImagesFromBucket()
    {
        int[] ids = null;
        ArrayList<Integer> lstIds = new ArrayList<>();
        String searchParams;

        if(bucket != null)
        {
            searchParams = "bucket_display_name = \""+bucket+"\"";
        }
        else
        {
            return ids;
        }
        String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATE_TAKEN};
        String orderBy = MediaStore.Images.Media.DATE_TAKEN + " DESC";
        mCursor = getActivity().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                searchParams,
                null,orderBy);
        if(mCursor.moveToFirst())
        {
            do
            {
                int id = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media._ID));
                lstIds.add(id);
            }
            while(mCursor.moveToNext());
        }
        ids = new int[lstIds.size()];
        for(int i=0;i<ids.length;i++)
        {
            ids[i] = lstIds.get(i);
        }
        return ids;
    }


    public class ImageAdapter extends BaseAdapter {

        private LayoutInflater inflater;

        ImageAdapter() {
            inflater = LayoutInflater.from(getActivity());
        }

        @Override
        public int getCount() {
            return imageUrls.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            View view = convertView;
            if (view == null) {
                view = inflater.inflate(R.layout.item_grid_image, parent, false);
                holder = new ViewHolder();
                assert view != null;
                holder.imageView = (ImageView) view.findViewById(R.id.gridImageView);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            ImageLoader.getInstance()
                    .displayImage(imageUrls.get(position), holder.imageView, options, new SimpleImageLoadingListener() {

                    });

            return view;
        }
    }

    static class ViewHolder {
        ImageView imageView;
    }
}

Any help with understanding what I am doing wrong and how to fix it would be greatly appreciated.

Upvotes: 0

Views: 633

Answers (2)

Kaitis
Kaitis

Reputation: 638

SOLVED!

I changed this line

Uri imageURI = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID);

to

 Uri imageURI = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + imageID);

Upvotes: 1

RBK
RBK

Reputation: 2417

If your path having filename with extension like .png, .jpg or any than it is file path, in such case you have to add string as

"file://"+ filename.

And if your path not having any extension and not web url, so it is content uri, here you have to add

"content://"+filename.

For more click here.

Upvotes: 0

Related Questions