2011-11-29 63 views
2

我認爲這不會太難,但是在過去的幾個小時裏,我的頭撞到了桌子上,真的很感謝您的幫助。本質上,我想從一個url中獲取圖像,將其保存到內部存儲器(不是sd卡),並且能夠檢索該圖像並在稍後使用ImageView顯示它。在Android平板電腦(3.1)上寫入內容和從內部存儲器讀取圖像時遇到問題

這是我從一個網址,以獲取圖像,並將其寫入內存(URL存儲在「圖片」):

    String urlstring = pics[l][w]; 
       if (urlstring != null){ 
        try { 
         URL url = new URL(urlstring); 
         InputStream input = url.openStream(); 
         FileOutputStream output = openFileOutput(("specimage"+l) + ("" +w+".jpg"), MODE_PRIVATE); 


         byte[] buffer = new byte[input.available()]; 
         int n = input.read(buffer, 0, buffer.length); 
         while (n >= 0) { 
          output.write(buffer, 0, buffer.length); 
          n = input.read(buffer, 0, buffer.length); 
         } 
         output.close(); 
         input.close(); 
        } catch (Exception e) { 
         GlobalState.popupMessage(homePage, "Error", "Files could not be stored on disk"); 
        } 
       } 

這是我試圖找回它們(路徑名) :

private Bitmap getPic(String path){ 
    FileInputStream in; 
    Bitmap bMap = null; 
     BufferedInputStream buf; 
     try { 
      in = openFileInput(path); 
      buf = new BufferedInputStream(in); 
      byte[] bMapArray= new byte[buf.available()]; 
      buf.read(bMapArray); 
      bMap = BitmapFactory.decodeStream(buf); 
      if (in != null) { 
      in.close(); 
      } 
      if (buf != null) { 
      buf.close(); 
      } 
     } catch (Exception e) { 
      System.out.println("excep."); 
     } 
     if (bMap == null) System.out.println("null"); 
    return bMap; 
} 

如果我這樣做,圖片不顯示,但程序不會崩潰。異常不會被觸發。但是,bMap的值是空的。我在日誌中也得到這個奇怪的消息:

DEBUG/Skia的(19358):--- SkImageDecoder ::廠返回null

請讓我知道我做錯了。我一直在洗劫我的大腦無濟於事。 我應該提到我在ui線程中執行setImageBitmap。

+0

你可以使用Log.e(),並讓我們知道你做 –

+0

我沒有得到確切的異常一個例外。它只是沒有顯示出來。 –

回答

1

剛剛嘗試這一點,(與您的替換),並讓我知道發生什麼事,

ImageView img = (ImageView)findViewById(R.id.imgView1); 
FileInputStream in; 
    Bitmap bMap = null; 
     BufferedInputStream buf; 
     try { 
      in = openFileInput("icon.png"); 
      buf = new BufferedInputStream(in); 
      byte[] bMapArray= new byte[buf.available()]; 
      buf.read(bMapArray); 
      bMap = BitmapFactory.decodeByteArray(bMapArray,0,bMapArray.length); 
      img.setImageBitmap(bMap); 
      if (in != null) { 
      in.close(); 
      } 
      if (buf != null) { 
      buf.close(); 
      } 
     } catch (Exception e) { 
      System.out.println("excep."); 
     } 
+0

沒有運氣。我得到這個消息:DEBUG/skia(19639):--- decoder-> decode返回false。此外bMap又是空的。 –

+0

我試過編輯過的一樣東西。 –

+0

在我的情況下你的代碼工作正常。 – user370305