无法加载到缩略图的ListView [英] Unable to load thumbnails into ListView

查看:177
本文介绍了无法加载到缩略图的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前试图做一个简单的图片库,如现在去precated的Andr​​oid 图库。从这个画廊的用户还可以通过选择图像格式的库定位到一个简单的图像编辑器。很多谷歌搜索后我设法找到一个 Horizo​​ntalListView 这里这正是我需要的。最初,我有很多的成功与此只要插入图像在我的文件夹中的COM pressed位图。从那时起,但我发现从谷歌视频I / O 在其中创建一个图片库和图像编辑器;类似于我试图创造。然而有我的两个应用程序,和他们之间的主要区别:

I am currently attempting to make a simple image gallery like the now deprecated Android Gallery. From this gallery the user can also navigate to a simple image editor by selecting an image form the gallery. After a lot of Googling I managed to find a HorizontalListView here which is exactly what I need. Initially I had a lot of success with this by just inserting the images in my folder as compressed bitmaps. Since then however I have found this video from Google I/O in which they create an image gallery and an image editor; similar to what I am attempting to create. There are however two main differences between my app and theirs:

  • 在他们使用的GridView 为他们的画廊和我使用上述 Horizo​​ntalListView
  • 在我试图只载入图像从指定的目标路径,而不仅仅是所有的SD卡上的图像。
  • they use a GridView for their gallery and I use the aforementioned HorizontalListView
  • I am attempting to only load images from a specified target path rather than just all images on the SD card.

到目前为止,我无法适应他们的code开采为无图像加载到我的画廊。作为与视频我使用的AsyncTask加载我的缩略图:

So far I am unable to adapt their code to mine as none of the images are loading into my gallery. As with the video I use an AsyncTask to load my thumbnails:

    private class ThumbnailAsyncTask extends AsyncTask<Long, Void, Bitmap>
    {
        //The ImageView we will be adding the thumbnail to
        private final ImageView mTarget;

        public ThumbnailAsyncTask(ImageView target)
        {
            mTarget = target;
        }

        @Override
        protected Bitmap doInBackground(Long... params) 
        {
            final long photoID = params[0];

            final Bitmap result = MediaStore.Images.Thumbnails.getThumbnail(
                    getContentResolver(), photoID, MediaStore.Images.Thumbnails.MINI_KIND, null);

            return result;
        }

        @Override
        protected void onPreExecute()
        {
            mTarget.setTag(this);
        }

        @Override
        protected void onPostExecute(Bitmap result)
        {
            if (mTarget.getTag() == this) 
            {
                mTarget.setImageBitmap(result);
                mTarget.setTag(null);
            }
        }   
}

和我使用的是CursorAdapter的在画廊的图片:

and I am using a CursorAdapter for the images in the gallery:

   private class PhotoAdapter extends CursorAdapter
   {
    public PhotoAdapter(Context context)
    {
        super(context, null, false);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) 
    {
        return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) 
    {
        final long photoId = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));

        final ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);

        /* 
         * Cancel any pending thumbnail task, since this view is now bound
         * to new thumbnail
         */
        final ThumbnailAsyncTask oldTask = (ThumbnailAsyncTask) imageView.getTag();

        if (oldTask != null) 
            oldTask.cancel(false);    

        /* 
         * If we arrived here, either cache is disabled or cache miss, so we
         * need to kick task to load manually
         */
        final ThumbnailAsyncTask task = new ThumbnailAsyncTask(imageView);
        imageView.setImageBitmap(null);
        imageView.setTag(task);
        task.execute(photoId);
    }
}

通过以​​下CursorLoader

With the following CursorLoader

    final LoaderCallbacks<Cursor> mCursorCallbacks = new LoaderCallbacks<Cursor>() 
            {
                @Override
                public Loader<Cursor> onCreateLoader(int id, Bundle args) 
                {
                    final String[] columns = {BaseColumns._ID};
                    return new CursorLoader(NewProjectActivity.this,
                            Uri.fromFile(new File(mTargetPath)), columns, null, null,
                            MediaStore.Images.Media.DATE_ADDED + " DESC");
                }

                @Override
                public void onLoadFinished(Loader<Cursor> loader, Cursor data) 
                {
                    mAdapter.swapCursor(data);
                }

                @Override
                public void onLoaderReset(Loader<Cursor> loader) 
                {
                    mAdapter.swapCursor(null);
                }
            };

    getLoaderManager().initLoader(LOADER_CURSOR, null, mCursorCallbacks);

这是为什么没有我的图片的加载任何想法?

Any ideas on why none of my images are loading?

推荐答案

这里的基本问题是, bindView()方法不等待的AsyncTask的结果。您需要通知您的列表视图时,它的一些内容得到改变。

The basic problem here is, bindView() method is not waiting for asynctask's result. You need to notify your list view when some of its content get changed.

您可以做以下修改。

1)更改 task.execute(PHOTOID); task.execute(PHOTOID)获得(); 这将迫使你的 bindView()方法来等待,直到你得到你的形象。这种做法是不可取的,但它会帮助你了解确切的问题。

1) Change task.execute(photoId); to task.execute(photoId).get(); this will force your bindView() method to wait till you get your image. This approach is not advisable but it will help you to understand the exact problem.

2) onPOST等() 的AsyncTask 的,无效的列表视图中的旧的内容,并尝试以重新装入新的内容。

2) In onPost() of asynctask, invalidate your list view's old content and try to reload it with new content.

mAdapter.notifyDataSetChanged();
mAdapter.notifyDataSetInvalidated();

3)个人而言,我会建议你先完成所有的网络操作(即获取图像),通过改变你的code结构,然后尝试后以$ P $来设置您的适配器pfetched数据。这不是一个完美的解决方案,但它的工作在我的情况。

3) Personally i will suggest you to finish all network operation first (i.e. fetching images ) by changing your code structure and then after try to set your adapter with the prefetched data. This is not a perfect solution but it worked in my case.

这篇关于无法加载到缩略图的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆