我找到了解決方案。來自這些網址的圖片可以被解碼,但問題在於它太大了,所以它顯示得非常大,看起來好像沒有顯示。
首先,我們需要捕捉圖像的描述是這樣的:
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream((InputStream)new URL(url).getContent(), null, options);
然後擴展到所需的寬度/高度,reqHeight/reqWidth是通緝尺寸參數:
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height/(float)reqHeight);
}
else {
inSampleSize = Math.round((float)width/(float)reqWidth);
}
}
之後只是重複來自問題的代碼:
Bitmap bitmap = null;
options.inJustDecodeBounds = false;
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(imagePath).getContent(), null, options);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
現在我們可以將它保存到某個目錄:
File file = new File(some_path\image.png);
if (!file.exists() || file.length() == 0) {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
fos.flush();
形象現在保存,我們可以抓住它,並顯示在我們的ImageView稱爲圖像:
Bitmap bitmap = BitmapFactory.decodeFile(some_path\image.png);
image.setImageBitmap(bitmap);
你的代碼不能編譯 - image.setImageBitmap(bimage); =錯字。請複製並粘貼您的真實代碼。 – auval
評論#2:拆分「新的URL(imagePath).getContent()」,測試你是否得到空。粘貼堆棧跟蹤..(幫我們幫你) – auval
這是真正的代碼。在複製/粘貼該頂級代碼後,我手動添加了最後一行,因此我輸錯了它。我測試了新的URL(imagePath).getContent()並且它不返回null。這真是個奇怪的問題。如果第一個字母是大寫或小寫,這不應該很重要。 – Cristiano