如何使用包含图像 (*.jpg) 的文件夹中的缩略图填充图库?

发布于 2024-12-09 12:43:38 字数 3505 浏览 0 评论 0原文

这是我用来用缩略图填充我的画廊的代码。

//--------------------------------------------
        final Cursor cursor;
        final int columnIndex;

        Gallery g = (Gallery) findViewById(R.id.gallery);
        // request only the image ID to be returned
        String[] projection = {MediaStore.Images.Media._ID};
        // Create the cursor pointing to the SDCard

        cursor = managedQuery(
                //uu.build(),
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, 
                MediaStore.Images.Media.DATA + " like ? ",
                new String[] {"%MyIdentityPhotos%"},  
                null);
        cursor.moveToFirst();
        // Get the column index of the image ID
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

        g.setAdapter(new BaseAdapter() {
            public int getCount() {
              return cursor.getCount();
            }
            public Object getItem(int position) {

                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // obtain the image URI
                Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                String url = uri.toString();
                // Set the content of the image based on the image URI
                int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
                return MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                                originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);

            }

            public long getItemId(int position) {
              return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(ImViewer.this);
                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // obtain the image URI
                Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                String url = uri.toString();
                // Set the content of the image based on the image URI
                int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
                Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                                originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
                i.setImageBitmap(b);

                Display display = getWindowManager().getDefaultDisplay(); 
                int width = display.getWidth();
                //int height = display.getHeight();

                i.setLayoutParams(new Gallery.LayoutParams(width, (int) (width / 0.77) ));
                i.setScaleType(ImageView.ScaleType.CENTER_CROP );
                //i.setBackgroundResource(mGalleryItemBackground);
                return i;
            }

          });
        //--------------------------------------------

这对我来说不好,因为这只会检索 Android 内置图库中的图像。

我正在寻找的是做同样的事情,但使用特定的文件夹。

提前致谢

This is my code that I am using to populate my gallery with thumnails.

//--------------------------------------------
        final Cursor cursor;
        final int columnIndex;

        Gallery g = (Gallery) findViewById(R.id.gallery);
        // request only the image ID to be returned
        String[] projection = {MediaStore.Images.Media._ID};
        // Create the cursor pointing to the SDCard

        cursor = managedQuery(
                //uu.build(),
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, 
                MediaStore.Images.Media.DATA + " like ? ",
                new String[] {"%MyIdentityPhotos%"},  
                null);
        cursor.moveToFirst();
        // Get the column index of the image ID
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);

        g.setAdapter(new BaseAdapter() {
            public int getCount() {
              return cursor.getCount();
            }
            public Object getItem(int position) {

                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // obtain the image URI
                Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                String url = uri.toString();
                // Set the content of the image based on the image URI
                int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
                return MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                                originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);

            }

            public long getItemId(int position) {
              return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView i = new ImageView(ImViewer.this);
                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // obtain the image URI
                Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                String url = uri.toString();
                // Set the content of the image based on the image URI
                int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
                Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
                                originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
                i.setImageBitmap(b);

                Display display = getWindowManager().getDefaultDisplay(); 
                int width = display.getWidth();
                //int height = display.getHeight();

                i.setLayoutParams(new Gallery.LayoutParams(width, (int) (width / 0.77) ));
                i.setScaleType(ImageView.ScaleType.CENTER_CROP );
                //i.setBackgroundResource(mGalleryItemBackground);
                return i;
            }

          });
        //--------------------------------------------

It is not ok for me because this retreives only images whitch are into the android built in gallery.

Whatr I am looking for is to do the same thing but using a specific folder.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文