使用Universal Image Loader的FileNotFoundException

时间:2015-03-22 10:46:28

标签: android gridview universal-image-loader

这是我在Android上的第一个应用程序,我正在使用通用图像加载程序库将图像加载到GridView中。

我遇到的问题是

  

FileNotFoundException:内容没有条目:// media / external / images / thumbnails / ...(所有缩略图)

缩略图未加载到GridView中,但是当我点击gridItem时,会启动Intent并将imageId传递给下一个活动。

这是我正在使用的代码(基于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;
    }
}

任何有助于理解我做错了什么以及如何解决问题的帮助都将非常感激。

2 个答案:

答案 0 :(得分:1)

解决!

我改变了这一行

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

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

答案 1 :(得分:0)

如果您的路径具有扩展名如.png,.jpg或其任何文件路径的文件名,则在这种情况下您必须添加字符串

"file://"+ filename.

如果你的路径没有任何扩展名而不是网址,那么它就是内容uri,在这里你必须添加

"content://"+filename.

此处有更多click