2013-03-27 62 views
2

我想從圖庫中選取圖片,將其轉換爲Bitmap,並將其顯示在ImageView中。選擇圖庫圖像並轉換爲位圖?

下面的代碼被用於拾取圖像從圖庫

Intent pickPhoto = new Intent(
      Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    startActivityForResult(pickPhoto, 1);` 

onActivityResult我將其轉換成一個Bitmap並在ImageView

protected void onActivityResult(int requestCode, int resultCode, 
     Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
    switch (requestCode) { 
    case 1: 
     if (resultCode == RESULT_OK) { 
      Uri selectedImage = imageReturnedIntent.getData(); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 4;//returning null for below statement 
      bitmap = BitmapFactory.decodeFile(selectedImage.toString(), options); 
      productItemImage.setImageBitmap(bitmap); 
     } 
     break; 
    } 
} 

值顯示它在所選圖像中Uri返回爲「content:// media/external/images/media/3647」

productItemImage is m y ImageView我正在顯示Bitmap。沒有錯誤,但位圖返回null。

請幫助

回答

-1

的問題可能是在您onActivityResult()函數的調用super.onActivityResult()

這是我去年寫的一個答案,它給出了你想要做的完整的例子,還允許你從相機以及圖庫中選擇:Allow user to select camera or gallery for image(這個答案有很高的分數,所以我覺得這對你會有所幫助)。

+0

對不起,這是一個打字錯誤的問題..我已糾正它..但問題仍然存在 – 2013-03-27 17:47:26

+0

檢查編輯我的答案,並轉到這裏:http://stackoverflow.com/questions/4455558/allow -user至選擇 - 照相機或畫廊換圖像/ 12347567#12347567 – 2013-03-27 18:15:39