0

我有一个从服务器相册加载图像的galleyview。但我的相册有很多图片 [超过 500 张]。所以一旦我更快地滚动galleyview,后台任务的数量就会越来越多,所以应用程序会崩溃。所以我打算根据galleyview中的位置杀死[取消]一些旧任务。所以请提出一些解决方案。下面提供了源代码。

任务调用:

DownloadImageTask downloadTask = new DownloadImageTask(                             

ShowGallery.this, view,position);

// cancel some task to avoid the crash - need to implement

// cancelPotentialDownload(position);

downloadTask.execute( THUMB_PREFIX + picture.getFileName(),
picture.getForceExtension(), thumbUrl,albumName, bitmapsCache, position, picture,null);


private static boolean cancelPotentialDownload(int position) {

// need to implement.       

}

下载图片任务

public class DownloadImageTask extends AsyncTask<Object, Void, Bitmap> {
    Context activity;
    private ImageView view;
    public int position;

    public DownloadImageTask(Context context, ImageView imageView, int imagePosition) {
        super();
        activity = context;
        view = imageView;
        position = imagePosition;
    }

    @Override
    protected Bitmap doInBackground(Object... parameters) {
        String fileName = (String) parameters[0];
        String extension = (String) parameters[1];
        String thumbUrl = (String) parameters[2];
        Integer currentAlbumName = (Integer) parameters[3];
        Map<Integer, Bitmap> bitmapsCache = (Map<Integer, Bitmap>) parameters[4];
        Integer position = (Integer) parameters[5];
        Picture picture = (Picture) parameters[6];
        Album album = (Album) parameters[7];

        Bitmap downloadImage = null;
        File imageFileOnExternalDirectory = null;
        try {
            imageFileOnExternalDirectory = FileUtils.getInstance()
                    .getFileFromGallery(activity, fileName, extension,
                            thumbUrl, true, currentAlbumName);
            downloadImage = BitmapFactory
                        .decodeFile(imageFileOnExternalDirectory.getPath());
            if (picture != null) {
                // only for showgallery activity
                picture.setThumbImageCachePath(imageFileOnExternalDirectory
                        .getPath());
                bitmapsCache.put(position, downloadImage);
            } else if (album != null) {
                // only for albumadapter
                album.setAlbumCoverCachePath(imageFileOnExternalDirectory
                        .getPath());
            }
        } catch (GalleryConnectionException e) {

            // Log.v(TAG, e.getMessage());
        } catch (FileHandlingException e) {

            // Log.v(TAG, e.getMessage());
        }

        return downloadImage;
    }

    @Override
     protected void onPostExecute(Bitmap downloadImage) {

        if (downloadImage != null) {
            view.setImageBitmap(downloadImage);


        }
    }

}
4

1 回答 1

0

查看此链接上的示例。您不是从 Web 下载图像,因此只需将此功能替换为从图库中读取图像

于 2013-08-21T13:21:46.057 回答