2012-03-03 29 views
0

嗨,我想在SD卡上指定一個文件夾來檢索該文件夾(例如,/ mnt/sdcard/Folder/SubFolder)的子文件夾中的所有圖像。我有這樣的:在MediaStore中指定一個文件夾managedQuery

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gallery); 
    GridView gridview = (GridView) findViewById(R.id.gallery); 
    registerForContextMenu(gridview); 

    id = getIntent().getExtras().getString("ID"); 

    // Set up an array of the Thumbnail Image ID column we want 
    String[] projection = {MediaStore.Images.Thumbnails._ID}; 
    // Create the cursor pointing to the SDCard 
    cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
      projection, // Which columns to return 
      null,  // Return all rows 
      null, 
      MediaStore.Images.Thumbnails.IMAGE_ID); 
    // Get the column index of the Thumbnails Image ID 
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 

在我的GridView的setAdapter我有:

gridview.setAdapter(new BaseAdapter() { 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView picturesView; 
      if (convertView == null) { 
       picturesView = new ImageView(mContext); 
       // Move cursor to current position 
       cursor.moveToPosition(position); 
       // Get the current value for the requested column 
       int imageID = cursor.getInt(columnIndex); 
       // Set the content of the image based on the provided URI 
       picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID)); 
       picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
       picturesView.setPadding(8, 8, 8, 8); 
       picturesView.setLayoutParams(new GridView.LayoutParams(100, 100)); 
      } 
      else { 
       picturesView = (ImageView)convertView; 
      } 
      return picturesView; 
     } 

     public int getCount() { 
      return cursor.getCount(); 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

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

    }); 

那的作品,但告訴我包含在SD卡所有圖片... 我試圖設置一個URI路徑,如「內容:/// mnt/sdcard /文件夾/子文件夾」但它doesen't工作...我怎麼能指定,在MediaStore managedQuery,特定文件夾掃描?

謝謝大家提前! :)

回答

3

嘗試獲取MediaStore.Images.Media.DATA字段 - 它包含圖像文件的完整路徑。然後,您可以運行光標並僅選擇您感興趣的文件或創建查詢,以便僅返回路徑爲「LIKE%your_folder_name%」的條目

相關問題