【问题标题】:load thumbnails with Universal Image Loader for Android?使用适用于 Android 的 Universal Image Loader 加载缩略图?
【发布时间】:2013-12-26 04:43:41
【问题描述】:

是否可以使用适用于 android 的 Universal image Loader 加载缩略图?。我在 Sdcard 中获得了图像和视频文件的缩略图,然后我不知道如何在 gridview 中使用这个库来显示这些缩略图,所以请告诉我有人知道该怎么做吗?

我从这段代码中得到了缩略图:

protected Integer doInBackground(Integer... params) {
        // TODO Auto-generated method stub

        final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };           // Images getting
        final String orderBy = MediaStore.Images.Media.DATE_TAKEN;

        imagecursor =  mContext.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, orderBy);
        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
        this.count = imagecursor.getCount();
        /*this.count = params[0];

        if(count == 0)
            this.count = imagecursor.getCount();
        else if(count >= 12)

        */
        bitList = new ArrayList<Bitmap>();
        arrPathList = new ArrayList<String>();
        selectedPath = new ArrayList<String>();

        for (int i = 0; i < this.count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);

            bitList.add( MediaStore.Images.Thumbnails.getThumbnail(
                    mContext.getContentResolver(), id,
                    MediaStore.Images.Thumbnails.MICRO_KIND, null));

            arrPathList.add(imagecursor.getString(dataColumnIndex));
        }

        this.durationcount = new ArrayList<String>();                                                  // Video Getting
        final String[] parameters = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, MediaStore.Video.Media.DURATION , MediaStore.Video.Media.MIME_TYPE}; // Videos getting
        final String orderBy_v = MediaStore.Video.Media._ID;

        videocursor = mContext.getContentResolver().query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, parameters, null,
                null, orderBy_v);

        int video_column_index = videocursor.getColumnIndex(MediaStore.Video.Media._ID);
        int video_column_duration = videocursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.DURATION);           // for duration of the video
        totalCount = imagecursor.getCount() + videocursor.getCount();                         /// Checking
        durationcount_a = new String[imagecursor.getCount() + videocursor.getCount()];

        for(int i = 0; i < videocursor.getCount(); i ++){
            videocursor.moveToPosition(ii);
            int id_v = videocursor.getInt(video_column_index);
            int datacolumn_v = videocursor.getColumnIndex(MediaStore.Video.Media.DATA);
            long duration = videocursor.getInt(video_column_duration);             // getting duration of the every videos

            String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(duration), 
                    TimeUnit.MILLISECONDS.toMinutes(duration) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration)),
                    TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)));

            durationcount.add(hms);
            durationcount_a[(imagecursor.getCount()) + ii] = hms;           
            bitList.add(MediaStore.Video.Thumbnails.getThumbnail(mContext.getContentResolver(), id_v,
                    MediaStore.Video.Thumbnails.MICRO_KIND, null));
            arrPathList.add(videocursor.getString(datacolumn_v));
        }
        thumbnailsselection = new boolean[totalCount];
        return null;

    }

提前致谢。

【问题讨论】:

  • 谢谢你的回复其实我已经看过你的博客了,真的很好,但是你在 imageLoader.displayImage() 方法中传递了文件路径,我也尝试了图像和视频文件,但它只会显示图像文件的缩略图不是视频文件,我需要显示两个文件的缩略图如何使用这个库?

标签: android gridview thumbnails universal-image-loader


【解决方案1】:

您必须获取缩略图的路径或 uri 才能使用通用图像加载器加载它。

查看此内容以获取 uri:-

Get thumbnail Uri/path of the image stored in sd card + android

我也从事过相同类型的项目,并将其托管在 git-hub 上。我有两个版本,一个没有 ImageLoader,另一个有 imageloader。现在我只托管了前一个:-

这里是路径https://github.com/r4jiv007/CustomFilePicker.git

这是我使用的方法:-

private String getImageThumbnail(int id) {
        final String thumb_DATA = MediaStore.Images.Thumbnails.DATA;
        final String thumb_IMAGE_ID = MediaStore.Images.Thumbnails.IMAGE_ID;
        Uri uri = thumbUri;
        String[] projection = {thumb_DATA, thumb_IMAGE_ID};
        String selection = thumb_IMAGE_ID + "=" + id + " AND " + MediaStore.Images.Thumbnails.KIND + "=" + MediaStore.Images.Thumbnails.MINI_KIND;
        Cursor thumbCursor = getContentResolver().query(uri, projection, selection, null, null);

        String thumbPath = null;
        Bitmap thumbBitmap = null;
        if (thumbCursor != null && thumbCursor.getCount() > 0) {
            thumbCursor.moveToFirst();
            int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);

            thumbPath = thumbCursor.getString(thCulumnIndex);
/*
            Toast.makeText(getApplicationContext(),
                    thumbPath,
                    Toast.LENGTH_LONG).show();*/

            //  thumbBitmap = BitmapFactory.decodeFile(thumbPath);

        }
        Log.i("ImageMiniKind", thumbPath + "");
        return thumbPath;
    }

你必须使用:-

  imageLoader.displayImage("file://" + fileX.getmThumbPath() + "", imageView, options);

用于加载图像..还要注意有时没有图像缩略图!

用于加载视频文件的缩略图

private String getVideoThumbnail(int id) {
    final String thumb_DATA = MediaStore.Video.Thumbnails.DATA;
    final String thumb_VIDEO_ID = MediaStore.Video.Thumbnails.VIDEO_ID;
    Uri uri = MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI;
    String[] projection = {thumb_DATA, thumb_VIDEO_ID};
    String selection = thumb_VIDEO_ID + "=" + id + " AND " + MediaStore.Video.Thumbnails.KIND + "=" + MediaStore.Video.Thumbnails.MINI_KIND;
    Cursor thumbCursor = getContentResolver().query(uri, projection, selection, null, null);
    String thumbPath = null;
    //  Bitmap thumbBitmap = null;
    if (thumbCursor.moveToFirst()) {

        int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);

        thumbPath = thumbCursor.getString(thCulumnIndex);
    }
    return thumbPath;
}

现在您所要做的就是将路径传递给 imageloader 库

【讨论】:

  • 感谢您的回复,我已经在您的方法中工作过,这意味着我刚刚传递了图像和视频文件的路径,但我只获得了图像文件的缩略图,所以我现在需要这两个文件的缩略图使用这个库加载这些缩略图,我想知道是否可能,让我知道如何实现它?
  • 不,这是我第一次在项目中使用这个库,我一直在为 sdcard 中的图像和视频文件获取拇指,这可以在 asyntask 中实现,但是在其中检索所有如果移动设备中的文件超过 100 个,则为缩略图,这就是为什么我使用这个库来更快地检索拇指
  • 你现在实现了多少?
  • 我几乎完成了,但我不知道如何在此方法中传递缩略图而不是路径 - imageLoader.displayImage("file://" + fileX.getmThumbPath() + "" ,图像视图,选项);或者有任何其他方法来使用这个库创建视频缩略图?
  • 我已经这样做了,现在可以使用 imageloader lib 传递视频路径,我无法在网格视图中获取缩略图,但我很容易获得图像文件的缩略图为什么会发生这种情况以及如何获取视频文件的缩略图使用这个库?
猜你喜欢
  • 2013-02-22
  • 2020-03-24
  • 2013-10-17
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
相关资源
最近更新 更多