2012-08-04 160 views
1

我一直在試圖製作一個小畫廊,並且位圖總是返回null。代碼是這樣的:位圖總是在android中返回null

public View getView(int position, View convertView, ViewGroup parent) { 

     ImageView i = new ImageView(mContext); 
     //Toast.makeText(getApplicationContext(),imgArray2.length+ " Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show(); 
     //Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position]); 
     //Uri uri = Uri.parse(imgArray2[position]); 
     //Bitmap bitmap = decodeFile(new File(uri.toString()).getAbsoluteFile()); 
     //Bitmap bitmap = BitmapFactory.decodeFile(uri.toString()); 
     //int imgID = getResources().getIdentifier(path, "drawable", "mypack.pack"); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 15; 
     Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position], options); 
     //i.setImageResource(imgArray2[position]); 
     i.setImageBitmap(bitmap); 
     //Uri uri = Uri.parse(imgArray2[position]); 
     Toast.makeText(getApplicationContext(), "Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show(); 
     //i.setImageURI(Uri.parse(imgArray2[position])); 
     i.setLayoutParams(new Gallery.LayoutParams(170, 170)); 
     i.setScaleType(ImageView.ScaleType.FIT_XY); 
     i.setBackgroundResource(mGalleryItemBackground); 
     return i; 
    } 

正如你可以看到的註釋掉的代碼,我一直在嘗試很多選項。 setImageURI的作品,但我需要縮小圖像,因爲我有很多。圖像在SD卡中。我檢查了圖像的路徑,它是正確的。這裏做錯了什麼?

+0

添加從logcat的輸出 – Frohnzie 2012-08-04 16:14:29

回答

0

您是否分析過日誌?有時,當圖像太大而無法解碼時,Dalvik虛擬機會拒絕內存請求,導致返回空圖像。

+0

我怎樣才能縮小呢?即使我有低分辨率圖片,它仍然返回null – Jonathan 2012-08-09 11:58:21

+0

嘗試閱讀位圖文檔。 @Jonathan – JoxTraex 2012-08-09 13:11:58

相關問題