2

我與我的Android應用程序代碼掙扎......Android的下載圖像通過intentservice OK,但不能將其設置在ImageView的

我試圖下載一個圖片(通過intentService),然後以顯示它將一個ImageView添加到LinearLayout。

到目前爲止,我已經通過管理下載的intentservice向我發送了絕對路徑。我得到路徑名稱的祝酒。

但我無法通過添加ImageView的顯示畫面......我真的不知道爲什麼......

起初,我以爲我的處理程序無法達到佈局,但我設法使用setImageResource顯示我的資源的隨機圖像。所以我想問題就來了無論是從setimagebitmap或bitmapfactory解碼...

在我Intentservice我有(輸出爲我的文件的名稱):

Messenger messenger = (Messenger) extras.get("MESSENGER"); 
Message msg = Message.obtain(); 
msg.arg1 = result; 
msg.obj = output.getAbsolutePath(); 
try { 
    messenger.send(msg); 
} etc... 

回到我的主要活動,我有:

Private Handler handler = new Handler() { 
    public void handleMessage(Message message) { 

     // I Get the message 
     Object path = message.obj; 

     //Check if download went ok 
     if (message.arg1 == RESULT_OK && path != null) { 
     Toast.makeText(Update3sur3.this, 
      "Downloaded" + path.toString(), Toast.LENGTH_LONG) 
      .show(); 

      //Try to display the pic 
      LinearLayout res2=(LinearLayout)findViewById(R.id.reslayout2); 
     ImageView imgView2 = new ImageView(Update3sur3.this); 
     Bitmap bitmap= BitmapFactory.decodeFile(path.toString()); 
     imgView2.setImageBitmap(bitmap); 
     res2.addView(imgView2); 
    } else { 
     Toast.makeText(Update3sur3.this, "Download failed.", 
      Toast.LENGTH_LONG).show(); 
     } 
    }; 
    }; 

非常感謝!

洛朗

+0

您面臨的問題是什麼? 是強制關閉還是圖像不顯示? – 2013-02-20 22:13:57

回答

1

起初我會盡量確保如果我能正確即訪問該文件,請按照下列步驟操作:

  1. 使用decodeByteArray(字節[]的數據,詮釋抵消,詮釋長度)方法而不是BitmapFactory.decodeFile(path.toString())

  2. 要使用這種新方法,你必須自己讀取文件的字節數組,這將會增加額外的步驟,但是你可以確定你正在正確讀取文件。

  3. 使用的FileInputStream要讀取的字節

文件鏡像文件=新的文件(路徑); byte imageData [] = new byte [(int)imageFile.length()];

的BufferedInputStream在新= BufferedInputStream爲(新的FileInputStream(鏡像文件);

in.read(的imageData,0,(INT)imageFile.length());

Log.d( 「」,「圖像文件大小:「+ imageFile.length()); //在這裏檢查文件大小是否正確?

//現在imageData是您將用來生成圖像的字節數組: decodeByteArray(imageData ,0,(int)imageFile.length());

如果圖像仍然沒有出現,那麼把日誌報告和打印圖像的大小,看看它是否正確。

+0

它與FileInputStream一起工作。謝謝。 – 2013-02-21 04:00:19

+0

歡迎您。請接受答案。 – 2013-02-21 09:13:53

+0

@LaurentMilleron我很高興它解決了你的問題。如果你發現有用的東西然後upvote,如果它已經解決了你的問題,然後接受答案。 – 2013-02-21 09:22:42

相關問題