2011-12-14 67 views
0

如何向用戶提供從相機或相冊中選擇圖像並檢索相關資源ID的選項?從相機或相冊中選擇一張圖片

+0

看看這個答案與合併在一個獨特的意圖都請求(相機和圖庫)的意圖:http://stackoverflow.com/a/32475805/2232889 – 2015-09-09 09:30:40

回答

5

嘗試以此爲目的

Intent i = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 

這裏是你如何retreive返回的圖像。

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

switch(requestCode) { 
case REQ_CODE_PICK_IMAGE: 
    if(resultCode == RESULT_OK){ 
     Uri selectedImage = imageReturnedIntent.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

     Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String filePath = cursor.getString(columnIndex); 
     cursor.close(); 


     Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); 
    } 
} 
} 
+0

你也可以在這裏得到更多的信息http://stackoverflow.com/questions/2507898/how-to-pick-a-image-from-gallery-sd-card-for-my-app-in-android – 2011-12-14 00:45:40

相關問題