当前位置: 首页 > 知识库问答 >
问题:

Android在图库视图中显示特定文件夹中的图像

郭德惠
2023-03-14

我正在尝试做一个小应用程序,只显示图片从一个特定的文件夹在一个图库视图。我找到了几个例子,但每一个都只能显示一个图像。我将在下面发布的这个例子是一个很好的帮助,它做几乎完全是我想要它做的,我只需要改变它显示图像从特定的文件夹,而不是所有的文件夹。我花了几天的时间来尝试,但我似乎没有添加正确的代码。我觉得这是一件很简单的事情,我也错过了。如有任何帮助,我们将不胜感激!

public class AndroidCustomGallery extends Activity {

    private int count;
    private Bitmap[] thumbnails;
    private boolean[] thumbnailsselection;
    private String[] arrPath;
    private ImageAdapter imageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

                final String orderBy = MediaStore.Images.Media._ID;

                Cursor  imagecursor = 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.thumbnails = new Bitmap[this.count];       
                this.arrPath = new String[this.count];  
                this.thumbnailsselection = new boolean[this.count];

                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); 

                  thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(        
                            getApplicationContext().getContentResolver(), id,       
                            MediaStore.Images.Thumbnails.MICRO_KIND, null);     

                    arrPath[i]= imagecursor.getString(dataColumnIndex);

                }

                GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);      
                imageAdapter = new ImageAdapter();      
                imagegrid.setAdapter(imageAdapter);     
                imagecursor.close();

共有1个答案

董良策
2023-03-14

想通了!下面是为任何想做类似事情的人粘贴的代码。

Cursor imagecursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        columns, 
                        MediaStore.Images.Media.DATA + " like ? ",
                        new String[] {"%/yourfoldername/%"},  
                        null);
 类似资料: