使用gridview显示SDCard上特定文件夹的图像

我正在尝试创建一个gridview,它载有来自SDCard上的特定文件夹的图像。该文件夹的路径是已知的,(“/ sdcard / pictures”),但在我在网上看到的示例中,我不确定如何或在何处指定图片文件夹的路径,我想从中加载图像。我已阅读了几十个教程,甚至是developer.android.com上的HelloGridView教程,但这些教程并没有教会我的内容。 我到目前为止阅读的每个教程都有: A)从/ res文件夹中将图像称为Drawable,并将它们放入要加载的数组中,而不是使用SDCard。 B)使用MediaStore访问SDCard上的所有图片,但未指定如何设置我想要显示图像表单的文件夹的路径 要么 C)建议使用BitmapFactory,我没有丝毫的线索如何使用。 如果我以错误的方式解决这个问题,请告诉我,并指导我采取正确的方法来做我想做的事情。     
已邀请:
好的,经过多次迭代尝试后,我终于有了一个有效的例子,我想我会分享它。我的示例查询图像MediaStore,然后获取要在视图中显示的每个图像的缩略图。我将我的图像加载到Gallery对象中,但这不是此代码的工作要求: 确保在类级别定义的列索引具有Cursor和int,以便Gallery的ImageAdapter可以访问它们:
private Cursor cursor;
private int columnIndex;
首先,获取位于文件夹中的图像ID的光标:
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( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection, 
        MediaStore.Images.Media.DATA + " like ? ",
        new String[] {"%myimagesfolder%"},  
        null);
// Get the column index of the image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
g.setAdapter(new ImageAdapter(this));
然后,在Gallery的ImageAdapter中,获取要显示的缩略图:
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView i = new ImageView(context);
    // 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);
    i.setLayoutParams(new Gallery.LayoutParams(150, 100));
    i.setScaleType(ImageView.ScaleType.FIT_XY);
    i.setBackgroundResource(mGalleryItemBackground);
    return i;
}
我想这段代码中最重要的部分是managedQuery,它演示了如何使用MediaStore查询来过滤特定文件夹中的图像文件列表。     
您需要在developer.android.com上执行比GridView教程更多的步骤。使用以下教程 http://developer.android.com/resources/tutorials/views/hello-gridview.html 您需要添加一种方法来创建SD卡中文件的ImageView: 创建/添加一个Vector到您的类变量(以保存ImageViews列表):
private Vector<ImageView> mySDCardImages;
初始化向量:
mySDCardImages = new Vector<ImageView>();
创建一个加载图像的方法:
List<Integer> drawablesId = new ArrayList<Integer>();
int picIndex=12345;
File sdDir = new File("/sdcard/pictures");
File[] sdDirFiles = sdDir.listFiles();
for(File singleFile : sdDirFiles)
{
   ImageView myImageView = new ImageView(context);
   myImageView.setImageDrawable(Drawable.createFromPath(singleFile.getAbsolutePath());
   myImageView.setId(picIndex);
   picIndex++;
   drawablesId.add(myImageView.getId());
   mySDCardImages.add(myImageView);
}
mThumbIds = (Integer[])drawablesId.toArray(new Integer[0]);
然后在ImageAdapter方法中进行更改
imageView.setImageResource(mThumbIds[position]);
imageView.setImageDrawable(mySDCardImages.get(position).getDrawable());
从ImageAdapter中删除mThumbIds的初始化。 (它应该与mySDCardImages的定义一致。可以访问这两个类方法。) (快速和脏版)确保测试您的路径等,并捕获任何例外。     
在你的情况下,BitmaFactory可能是一个很好的方法。例:
File dir = new File( "/sdcard/pictures" );    
String[] fileNames = dir.list(new FilenameFilter() { 
  boolean accept (File dir, String name) {
      if (new File(dir,name).isDirectory())
         return false;
      return name.toLowerCase().endsWith(".png");
  }
});
for(string bitmapFileName : fileNames) {
  Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" + bitmapFileName);
  // do something with bitmap
}
没时间测试这个但应该工作;-)     
阅读此链接:http://androidsamples.blogspot.com/2009/06/how-to-display-thumbnails-of-images.html 它显示了如何使用mediastore和bitmapfactory。 你应该选择的方式取决于你究竟需要什么。如果你有一组静态图像,最好将它们放到drawables中,我认为,这样做会更快,你不依赖SD卡,它可以删除,损坏或文件可以重命名/删除 如果图像是动态的,则使用mediastore或bitmap factory。但请记住,将图像放入数组或其他内存耗费的东西,所以你最终可能会出现内存异常     
看起来你想要自定义画廊,这将花费你很多时间, 我建议您使用自定义相机库进行工作。 您可以根据需要在网格视图中获取照片/视频。     
对于Kotlin代码,请参阅此问题的答案 这个想法是alslo适用于java(但你需要修改代码)     

要回复问题请先登录注册