bugfix> android > 投稿

私はrecyclerviewにgridviewを実装し、画像と動画をサムネイルで表示しました。 私は次のコードを使用して画像を取得しました onBindView  方法

以下のコードを使用して画像のサムネイルを取得する:

bitmap = MediaStore.Images.Thumbnails.getThumbnail(context
                        .getApplicationContext().getContentResolver(), item.getImgId(),
                MediaStore.Images.Thumbnails.MICRO_KIND, null);

以下のコードを使用してビデオのサムネイルを取得する:

 bitmap = MediaStore.Video.Thumbnails.getThumbnail(context
                       .getApplicationContext().getContentResolver(), item.getImgId(),
               MediaStore.Images.Thumbnails.MICRO_KIND, null);

しかし、ビューをスクロールするたびに(スクロールダウンまたはアップするには時間がかかります)。サムネイルを読み込んでいるからだと思う onBindView  方法。

このコードを改善して、スムーズにスクロールできるようにしてください。 または簡単に行うためのAPI。

回答 1 件
  • 以下のコードをonCreateに追加します。

    imageAdapter = new ImageAdapter();
        imageAdapter.initialize();
    
    

    ローカルストレージから画像とビデオを取得するためのアダプタは次のとおりです。

     public class ImageAdapter extends BaseAdapter {
            private LayoutInflater mInflater;
            public ArrayList<ImageItem> images = new ArrayList<ImageItem>();
            public ImageAdapter() {
                mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }
            public void initialize() {
                images.clear();
                final String[] columns = {MediaStore.Images.Thumbnails._ID};
                final String orderBy = MediaStore.Images.Media._ID;
                Cursor imagecursor = managedQuery(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                        null, null, orderBy);
    
                int int_position = 0;
                Uri uri;
                Cursor cursor;
                int column_index_data, column_index_folder_name, column_id, thum;
                String absolutePathOfImage = null;
                uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME, MediaStore.Video.Media._ID, MediaStore.Video.Thumbnails.DATA};
                cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
                column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
                column_id = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
                thum = cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);
                while (cursor.moveToNext()) {
                    int id = cursor.getColumnIndex(MediaStore.Video.Media._ID);
                    absolutePathOfImage = cursor.getString(column_index_data);
                    Log.e("Column", absolutePathOfImage);
                    Log.e("Folder", cursor.getString(column_index_folder_name));
                    Log.e("column_id", cursor.getString(column_id));
                    Log.e("thum", cursor.getString(thum));
                    ImageItem obj_model = new ImageItem();
                    obj_model.id = id;
                    obj_model.thumb = cursor.getString(thum);
                    images.add(obj_model);
                }
                if (imagecursor != null) {
                    int image_column_index = imagecursor
                            .getColumnIndex(MediaStore.Images.Media._ID);
                    int count = imagecursor.getCount();
                    for (int i = 0; i < count; i++) {
                        imagecursor.moveToPosition(i);
                        int id = imagecursor.getInt(image_column_index);
                        ImageItem imageItem = new ImageItem();
                        imageItem.id = id;
                        lastId = id;
                        imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
                                getApplicationContext().getContentResolver(), id,
                                MediaStore.Images.Thumbnails.MICRO_KIND, null);
                        images.add(imageItem);
                    }
                    imagecursor.close();
                }
                notifyDataSetChanged();
            }
            public void checkForNewImages() {
                //Here we'll only check for newer images
                final String[] columns = {MediaStore.Images.Thumbnails._ID};
                final String orderBy = MediaStore.Images.Media._ID;
                Cursor imagecursor = managedQuery(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                        MediaStore.Images.Media._ID + " > " + lastId, null, orderBy);
                int image_column_index = imagecursor
                        .getColumnIndex(MediaStore.Images.Media._ID);
                int count = imagecursor.getCount();
                for (int i = 0; i < count; i++) {
                    imagecursor.moveToPosition(i);
                    int id = imagecursor.getInt(image_column_index);
                    ImageItem imageItem = new ImageItem();
                    imageItem.id = id;
                    lastId = id;
                    imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
                            getApplicationContext().getContentResolver(), id,
                            MediaStore.Images.Thumbnails.MICRO_KIND, null);
                    imageItem.selection = true; //newly added item will be selected by default
                    images.add(imageItem);
                }
                imagecursor.close();
                notifyDataSetChanged();
            }
            public int getCount() {
                return images.size();
            }
            public Object getItem(int position) {
                return position;
            }
            public long getItemId(int position) {
                return position;
            }
            public View getView(int position, View convertView, final ViewGroup parent) {
                final ViewHolder holder;
                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = mInflater.inflate(R.layout.galleryitem1, null);
                    holder.imageview = (ImageView) convertView
                            .findViewById(R.id.thumbImage);
                    holder.ivVideoIcon = (ImageView) convertView
                            .findViewById(R.id.ivVideoIcon);
                    holder.checkbox = (CheckBox) convertView
                            .findViewById(R.id.itemCheckBox);
                    holder.pbImageLoad = (ProgressBar) convertView
                            .findViewById(R.id.pbImageLoad);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                ImageItem item = images.get(position);
                holder.checkbox.setId(position);
                holder.imageview.setId(position);
    
                if (item.thumb != null) {
                    /*Picasso.with(AndroidCustomGalleryActivity.this)
                            .load("file" + item.thumb)
                            .into(holder.imageview);*/
                    holder.ivVideoIcon.setVisibility(View.VISIBLE);
                    Glide.with(AndroidCustomGalleryActivity.this).load(Uri.fromFile(new File(item.thumb)))
                            .listener(new RequestListener<Uri, GlideDrawable>() {
                                @Override
                                public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
                                    holder.pbImageLoad.setVisibility(View.GONE);
                                    return false;
                                }
                                @Override
                                public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                    holder.pbImageLoad.setVisibility(View.GONE);
                                    return false;
                                }
                            })
                            .skipMemoryCache(false)
                            .into(holder.imageview);
                } else {
    //                holder.imageview.setImageBitmap(item.img);
                    if (item.img != null) {
                        holder.ivVideoIcon.setVisibility(View.GONE);
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        item.img.compress(Bitmap.CompressFormat.PNG, 100, stream);
                        Glide.with(AndroidCustomGalleryActivity.this).load(stream.toByteArray()).asBitmap().listener(new RequestListener<byte[], Bitmap>() {
                            @Override
                            public boolean onException(Exception e, byte[] model, Target<Bitmap> target, boolean isFirstResource) {
                                holder.pbImageLoad.setVisibility(View.GONE);
                                return false;
                            }
                            @Override
                            public boolean onResourceReady(Bitmap resource, byte[] model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                holder.pbImageLoad.setVisibility(View.GONE);
                                return false;
                            }
                        }).skipMemoryCache(false).into(holder.imageview);
                    }
                }
                holder.checkbox.setChecked(item.selection);
                return convertView;
            }
        }
        class ViewHolder {
            ImageView imageview;
            ImageView ivVideoIcon;
            CheckBox checkbox;
            ProgressBar pbImageLoad;
        }
        class ImageItem {
            boolean selection;
            int id;
            Bitmap img;
            String thumb;
        }
    
    

あなたの答え