2011-06-21 76 views
1

我想要sdcard中的任何一個圖像。 我只知道image的名字。 但我如何檢索我的程序(包括路徑)內的特定圖像。也可以轉換成bitmap或存儲在位圖變量 我該怎麼做? 謝謝android如何獲取sd卡中的特定圖像

+0

你知道圖像或圖像名稱的完整路徑嗎? –

回答

1

在這裏,你可以顯示SD卡的所有圖像,當你點擊其中一個,你選擇圖片的路徑:

try { 
     // 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 sdcardImages = (GridView) findViewById(R.id.gallary); 
     sdcardImages.setAdapter(new ImageAdapter(this)); 

     // Set up a click listener 
     sdcardImages.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView parent, View v, 
        int position, long id) { 
       // Get the data location of the image 
       String[] projection = { MediaStore.Images.Media.DATA }; 
       cursor = managedQuery(
         MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
         projection, // Which columns to return 
         null, // Return all rows 
         null, null); 
       columnIndex = cursor 
         .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       cursor.moveToPosition(position); 
       // Get image filename 
       String imagePath = cursor.getString(columnIndex); 
       // Use this path to do further processing, i.e. full screen 
       // display 
      } 
     }); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

這裏是ImageAdapter:

/** 
* Adapter for our image files. 
*/ 
private class ImageAdapter extends BaseAdapter { 

    private Context context; 

    public ImageAdapter(Context localContext) { 
     context = localContext; 
    } 

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView picturesView; 
     if (convertView == null) { 
      picturesView = new ImageView(context); 
      // 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; 
    } 
} 

如果你知道確切的路徑和圖像名稱,您可以通過使用得到Bitmap

Bitmap bitmap = BitmapFactory.decodeFile("yourImageDirectory" + "yourImageName.png"); 
+0

我想知道sd卡中圖像的路徑是什麼? – Jyosna

+0

我知道。在'cursor'中有關於SDCard中圖像的所有信息。所以如果你循環使用光標,你可以得到你想要的完整圖像路徑。 –

+0

我查詢縮略圖圖像並在此變量中:String imagePath = cursor.getString(columnIndex); 我得到了:/mnt/sdcard/DCIM/.thumbnails/1329694769950.jpg女巫是通往縮略圖的路徑,而不是原始圖像。我想問你,如果你知道,我如何獲得完整圖像(原始圖像)的路徑?謝謝 –

相關問題