2013-11-25 31 views
4

嗨我正在开发android画廊应用程序。我从SD卡中的文件夹获取的图像,并在网格视图中显示它下面列出SD卡中的所有图像。

public static ArrayList<String> getFilePaths() 
{ 
    ArrayList<String> filePaths = new ArrayList<String>(); 

    File directory = new File(android.os.Environment.getExternalStorageDirectory() + File.separator + AppConstant.PHOTO_ALBUM); 

    // check for directory 
    if (directory.isDirectory()) 
    { 
     // getting list of file paths 
     File[] listFiles = directory.listFiles(); 

     // Check for count 
     if (listFiles.length > 0) 
     { 

      for (int i = 0; i < listFiles.length; i++) 
      { 

       String filePath = listFiles[i].getAbsolutePath(); 

       if (IsSupportedFile(filePath)) 
       { 
        // Add image path to array list 
        filePaths.add(filePath); 
       } 
      } 
     } 
     else 
     { 
      // image directory is empty 
      Toast.makeText(
        _context, 
        AppConstant.PHOTO_ALBUM 
        + " is empty. Please load some images in it !", 
        Toast.LENGTH_LONG).show(); 
     } 

    } 
    return filePaths; 
} 


//fetching all image paths 
imagePaths = utils.getFilePaths(); 
adapter = new GridViewImageAdapter(GridViewActivity.this, imagePaths, columnWidth); 
// setting grid view adapter 
gridView.setAdapter(adapter); 

我想不仅在指定的文件夹,显示所有从SD卡图像。我不知道该怎么做。

请帮忙。谢谢!

+0

您只能使用URI而不是文件路径。其更好的方法来从SD卡中获取图像 –

+0

http://mobilecomputing650003.wordpress.com/2013/10/15/mediastore-content-provider-to-retrieve-images-from-sdcard-and-displaying-in-gridview/ 。检查它是否有帮助 – Raghunandan

+0

更改为'File directory = Environment.getExternalStorageDirectory();'。我认为这应该工作。 –

回答

16

使用此方法。这将返回列出你的SD卡内的所有图像路径,如果你不想要任何指定的图像扩展,你可以过滤掉。

public ArrayList<String> getFilePaths() 
    { 


     Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
     String[] projection = {MediaStore.Images.ImageColumns.DATA}; 
     Cursor c = null; 
     SortedSet<String> dirList = new TreeSet<String>(); 
     ArrayList<String> resultIAV = new ArrayList<String>(); 

     String[] directories = null; 
     if (u != null) 
     { 
      c = managedQuery(u, projection, null, null, null); 
     } 

     if ((c != null) && (c.moveToFirst())) 
     { 
      do 
      { 
       String tempDir = c.getString(0); 
       tempDir = tempDir.substring(0, tempDir.lastIndexOf("/")); 
       try{ 
        dirList.add(tempDir); 
       } 
       catch(Exception e) 
       { 

       } 
      } 
      while (c.moveToNext()); 
      directories = new String[dirList.size()]; 
      dirList.toArray(directories); 

     } 

     for(int i=0;i<dirList.size();i++) 
     { 
      File imageDir = new File(directories[i]); 
      File[] imageList = imageDir.listFiles(); 
      if(imageList == null) 
       continue; 
      for (File imagePath : imageList) { 
       try { 

         if(imagePath.isDirectory()) 
         { 
          imageList = imagePath.listFiles(); 

         } 
         if (imagePath.getName().contains(".jpg")|| imagePath.getName().contains(".JPG") 
           || imagePath.getName().contains(".jpeg")|| imagePath.getName().contains(".JPEG")          
           || imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG") 
           || imagePath.getName().contains(".gif") || imagePath.getName().contains(".GIF") 
           || imagePath.getName().contains(".bmp") || imagePath.getName().contains(".BMP")       
     ) 
         { 



          String path= imagePath.getAbsolutePath(); 
         resultIAV.add(path); 

         } 
        } 
      // } 
      catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

     return resultIAV; 


    } 
+1

谢谢!这工作 – sanjana

+1

谢谢.......工作就像一个魅力......... –

0

使用内容提供商把所有的图片来自MediaStrore.Images.Media

public void fetchImageIds(){ 

    Cursor cursor = getContentResolver() 
         .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{}, null, null) 

    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     ids.add(cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID))); 
     cursor.moveToNext(); 

    } 
    adapter.notifyDataSetChanged(); 
} 

在GridView的适配器

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    ImageView imageView = new ImageView(SeeYourself.this); 
    Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(contentResolver, ids.get(position), MediaStore.Images.Thumbnails.MICRO_KIND, null); 

    imageView.setImageBitmap(bitmap); 

    return imageView; //To change body of implemented methods use File | Settings | File Templates. 
} 
0
/** 
* Getting All Images Path 
* 
* @param activity 
* @return ArrayList with images Path 
*/ 

public static ArrayList<String> getAllShownImagesPath(Activity activity) { 
    Uri uri; 
    Cursor cursor; 
    int column_index; 
    StringTokenizer st1; 
    ArrayList<String> listOfAllImages = new ArrayList<String>(); 
    String absolutePathOfImage = null; 
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 

    String[] projection = { MediaColumns.DATA }; 

    cursor = activity.getContentResolver().query(uri, projection, null, 
      null, null); 

    column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 
    while (cursor.moveToNext()) { 
     absolutePathOfImage = cursor.getString(column_index); 
     listOfAllImages.add(absolutePathOfImage); 
    } 

    return listOfAllImages; 
} 
0

这对我的作品

private void load_image_files(File dir) { 

    String extention = ".jpg"; 
    File[] listFile = dir.listFiles(); 
    if (listFile != null) { 
     for (int i = 0; i < listFile.length; i++) { 

      if (listFile[i].isDirectory()) { 
       load_image_files(listFile[i]); 
      } else { 
       if (listFile[i].getName().endsWith(extention)) { 
        name_list.add(listFile[i].getName()); 
        name_path_list.add(listFile[i].getAbsolutePath()); 

       } 
      } 
     } 
    } 
} 
0

使用this function它来自内部和外部存储卡返回图片列表

public static ArrayList<ImageDataModel> allImages = new ArrayList<ImageDataModel>(); 

放入的Util

/** 
     * Getting All Images Path. 
     * 
     * @param activity 
     *   the activity 
     * @return ArrayList with images Path 
     */ 
      public static ArrayList<ImageDataModel> gettAllImages(Activity activity) { 

       //Remove older images to avoid copying same image twice 

       allImages.clear(); 
       Uri uri; 
       Cursor cursor; 
       int column_index_data, column_index_folder_name; 

       String absolutePathOfImage = null, imageName; 

      //get all images from external storage 

      uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 

      String[] projection = { MediaColumns.DATA, 
        MediaStore.Images.Media.DISPLAY_NAME }; 

      cursor = activity.getContentResolver().query(uri, projection, null, 
        null, null); 

      column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 

      column_index_folder_name = cursor 
        .getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME); 

      while (cursor.moveToNext()) { 

       absolutePathOfImage = cursor.getString(column_index_data); 

       imageName = cursor.getString(column_index_folder_name); 

       allImages.add(new ImageDataModel(imageName, absolutePathOfImage)); 

      } 

      // Get all Internal storage images 

      uri = android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI; 

      cursor = activity.getContentResolver().query(uri, projection, null, 
        null, null); 

      column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 

      column_index_folder_name = cursor 
        .getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME); 

      while (cursor.moveToNext()) { 

       absolutePathOfImage = cursor.getString(column_index_data); 

       imageName = cursor.getString(column_index_folder_name); 

       allImages.add(new ImageDataModel(imageName, absolutePathOfImage)); 
      } 

      return allImages; 
     } 

    } 

模型类来保存图象 -

public class ImageDataModel { 

    private String imageTitle , imagePath; 

    /** 
    * @return the imageTitle 
    */ 
    public String getImageTitle() { 
     return imageTitle; 
    } 

    /** 
    * @param imageTitle the imageTitle to set 
    */ 
    public void setImageTitle(String imageTitle) { 
     this.imageTitle = imageTitle; 
    } 

    /** 
    * @return the imagePath 
    */ 
    public String getImagePath() { 
     return imagePath; 
    } 

    /** 
    * @param imagePath the imagePath to set 
    */ 
    public void setImagePath(String imagePath) { 
     this.imagePath = imagePath; 
    } 

    public ImageDataModel(String imageTitle, String imagePath) { 
     super(); 
     this.imageTitle = imageTitle; 
     this.imagePath = imagePath; 
    } 

} 

结果: -

Android gallery Sample

如果你仍然面临的问题完整的代码可以在Github上

https://github.com/hiteshsahu/AwesomeAndroid-Gallery