[問題] 網路下載東西時 前景影像不見

看板AndroidDev作者 (skill)時間13年前 (2012/09/18 09:54), 編輯推噓5(5016)
留言21則, 7人參與, 最新討論串1/1
想請問各位大大 我寫了一個程式 主要是 show 了一張圖片後 開了一個thread去網路載圖 但是當檔案很大的時候 前景影像卻消失了! (神奇) 可以請問這是甚麼樣的問題嗎? QQ 謝謝大大們! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.163.3.187

09/18 13:46, , 1F
沒程式碼 沒Log 我猜是"檔案很大"的問題
09/18 13:46, 1F
onCreate function: ImageView imageView = (ImageView)findViewById( R.id.imageView1); Bitmap bitmap = fileDealer.getImageBitmap(this); imageView.setImageBitmap(bitmap); onStart function: new Thread( new Runnable(){ public void run(){ URL url = new URL("檔案超過1mb的連結"); URLConnection conn = url.openConnection(); HttpURLConnection httpConn = (HttpURLConnection)conn; httpConn.connect(); if( httpConn.getResponseCode() == HttpURLConnection.HTTP_OK){ inputStream = httpConn.getInputStream(); } } }).start(); 我有想過是不是因為thread在跑的時候吃了大部分的資源 所以main thread就沒時間輸出畫面? ~..~ ※ 編輯: issuemylove 來自: 118.163.3.187 (09/18 14:16)

09/18 14:37, , 2F
何不用Handler
09/18 14:37, 2F

09/18 18:12, , 3F
用asyntask讓他到背景執行
09/18 18:12, 3F

09/19 00:11, , 4F
thread中沒看到把網路圖片存到機器上的code,所以會變黑?
09/19 00:11, 4F

09/19 11:44, , 5F
可是thread執行 不也是背景執行嗎? QQ
09/19 11:44, 5F

09/19 13:44, , 6F
inputStream = httpConn.getInputStream()這行執行完並不代
09/19 13:44, 6F

09/19 13:48, , 7F
表圖已經下載好,是後續使用inputStream資料時,才會開始從
09/19 13:48, 7F

09/19 13:49, , 8F
網路下載圖形
09/19 13:49, 8F

09/19 18:58, , 9F
我使用了AsyncTask之後 反而是執行一半直接關掉
09/19 18:58, 9F

09/19 18:58, , 10F
是因為下載太久了嗎? QQ
09/19 18:58, 10F

09/19 18:59, , 11F
AsyncTask就是用來跑長時間的,關掉是當掉還是直接消失?
09/19 18:59, 11F

09/19 19:07, , 12F
直接消失 error message http://ppt.cc/lEC1
09/19 19:07, 12F
我看官網AsyncTask好像也是說它比較適合跑short operation耶QQ http://developer.android.com/reference/android/os/AsyncTask.html AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask. ※ 編輯: issuemylove 來自: 118.163.3.187 (09/19 19:10)

09/19 20:06, , 13F
是喔,那我誤解了他的用法了
09/19 20:06, 13F

09/19 21:37, , 14F
感覺上不是執行太久的問題,是有東西為null,請debug看看
09/19 21:37, 14F

09/19 21:46, , 15F
File沒有new到或是找不到檔案之類的?
09/19 21:46, 15F

09/19 22:52, , 16F
也許你可以試試DownloadManager
09/19 22:52, 16F

09/20 01:27, , 17F
把全部的code貼出來看看,我猜問題出在沒貼出來的地方
09/20 01:27, 17F
這是錯誤發生的code http://ppt.cc/BqB3 http://ppt.cc/SicJ 錯誤訊息跟上面的一樣 http://ppt.cc/lEC1 我不知道為什麼會有null產生~..~ 因為跑小一點的檔案就可以 大的話就有null QQ||| ※ 編輯: issuemylove 來自: 118.163.3.187 (09/20 09:52) ※ 編輯: issuemylove 來自: 118.163.3.187 (09/20 09:56)

09/20 10:32, , 18F
.............大家對不起
09/20 10:32, 18F

09/20 10:33, , 19F
剛剛找出bug是server的連結搞錯Q.Q 因為我有兩個在用
09/20 10:33, 19F

09/20 10:33, , 20F
粗心的搞錯了T.T
09/20 10:33, 20F

09/20 10:34, , 21F
非常謝謝大大們的回應! QQ
09/20 10:34, 21F
文章代碼(AID): #1GLzHEdZ (AndroidDev)