2011-07-01 72 views
0

我正嘗試讀取SDCARD中存在目錄的所有圖像。例如:如果/ mnt/sdcard/album1中有TEST.jpg文件,/ mnt/sdcard/album1/album2中有TEST2.jpg文件,我應該能夠獲得目錄名稱album1和album2。 我已經寫了一個代碼,它以遞歸的方式做到這一點,當文件夾數量少時,但是當目錄數量增加時,循環剛剛出來。android讀取SDCARD中所有包含圖像的目錄

 public void getImageFoldes(String filepath){ 


      String albumpath; 
      File file = new File(filepath); 

    File[] files = file.listFiles(); 
    for (int fileInList = 0; fileInList < files.length; fileInList++) 
    { 
     File filename; 
     filename =files[fileInList]; 

     if(filename.isHidden()|| filename.toString().startsWith(".")) 
      return; 

     if (filename.isDirectory()){ 

      albumpath = filename.toString(); 
      String[] split; 
      String title; 
      split= albumpath.split("/"); 
      title=split[split.length-1]; 
      result = new thumbnailResults(); 
      result.setTitle(title); 
      result.setPath(albumpath); 
      result.setIsLocal(true); 
      //result.setCreated("05-06-2011"); 
      getImageFoldes(filename.toString()); 
     } 
     else{ 
      if (files.length !=0) 
      { 
       //if File is the image file then store the album name 
       if ((files[fileInList].toString()).contains(".png")|| 
         (files[fileInList].toString()).contains(".jpg")|| 
         (files[fileInList].toString()).contains(".jpeg")){ 
        if (!results.contains(result)){ 
         result.setUri(Uri.parse(files[fileInList].getPath())); 
         results.add(result); 
         myadapter.notifyDataSetChanged(); 

        } 
       }  
      } 
     } 
    } 
} 

回答

2

使用下面的代碼。 從sdcard獲取所有圖像和目錄的路徑。

public static ArrayList<String> getPathOfAllImages(Activity activity) { 
     ArrayList<String> absolutePathOfImageList = new ArrayList<String>(); 
     String absolutePathOfImage = null; 
     String nameOfFile = null; 
     String absolutePathOfFileWithoutFileName = null; 
     Uri uri; 
     Cursor cursor; 
     int column_index; 
     int column_displayname; 
     int lastIndex; 
     // absolutePathOfImages.clear(); 


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

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

      cursor = activity.managedQuery(uri, projection, null, null, null); 
      column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); 

      column_displayname = cursor 
        .getColumnIndexOrThrow(MediaColumns.DISPLAY_NAME); 

      // cursor.moveToFirst(); 
      while (cursor.moveToNext()) { 
       // for(int i=0; i<cursor.getColumnCount();i++){ 
       // Log.i(TAG,cursor.getColumnName(i)+".....Data Present ...."+cursor.getString(i)); 
       // } 
       // Log.i(TAG,"====================================="); 

       absolutePathOfImage = cursor.getString(column_index); 
       nameOfFile = cursor.getString(column_displayname); 

       lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile); 

       lastIndex = lastIndex >= 0 ? lastIndex 
         : nameOfFile.length() - 1; 

       absolutePathOfFileWithoutFileName = absolutePathOfImage 
         .substring(0, lastIndex); 


        if (absolutePathOfImage != null) { 
         absolutePathOfImageList.add(absolutePathOfImage); 
        } 

      } 


     // Log.i(TAG,"........Detected images for Grid....." 
     // + absolutePathOfImageList); 
     return absolutePathOfImageList; 
    } 
+0

sahoo:它給我的JPEG文件的確切文件路徑。我需要的只是直到目錄的路徑。假設我有一個文件夾/mnt/sdcard/album1/album2/test.jpg和/mnt/sdcard/album3/test1.jpg,那麼我的結果應該是包含第一個值的字符串的數組列表,其中/ man/sdcard/album1/album2和第二項將是/ mnt/sdcard/album3。爲什麼不搜索DCIM文件夾? – jhon

0

您可以通過SD卡中的所有圖像文件,它可能工作。

public class ReadallImagesActivity extends Activity { 

    ArrayList<String> arlist = new ArrayList<String>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     File ff = Environment.getExternalStorageDirectory(); 
     loadImagepaths(ff); 
     setContentView(R.layout.main); 
     Toast.makeText(ReadallImagesActivity.this, "Array size == " +arlist.size(), Toast.LENGTH_LONG).show(); 
    } 

    public void loadImagepaths(File file) { 
     for (File f : file.listFiles()) { 

      if (f.isDirectory()) { 
       if (f.getAbsolutePath().endsWith(".android_secure")) { 
        break; 
       } 
       if (f.getAbsolutePath().endsWith("DCIM")) { 
        continue; 
       } 
       loadImagepaths(f); 
      } else { 
       if (f.getAbsolutePath().endsWith(".png") || 
        f.getAbsolutePath().endsWith(".gif") || 
        f.getAbsolutePath().endsWith(".jpg")) 
       { 
        arlist.add(f.getAbsolutePath()); 
       } 
      } 
     } 
    } 
} 
相關問題