2016-01-05 53 views
-1

我正在一個Android應用程序,我試圖將InputStream解碼爲位圖。BitmapFactory.decodeStream拋出NetworkOnMainThreadException - Android

Bitmap bmp = BitmapFactory.decodeStream(s); 

以上內QBContent.downloadFileTask(...)拋繩NetworkOnMainThread例外。

下面是引用代碼:

getView()ChatAdapter

 @Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    final ViewHolder holder; 
    QBChatMessage chatMessage = getItem(position); 
    LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    int type = getItemViewType(position); 
    if (convertView == null) { 
     convertView = vi.inflate(R.layout.list_item_image, parent, false); 
     holder = createViewHolder(convertView); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    QBUser currentUser = ChatService.getInstance().getCurrentUser(); 
    boolean isOutgoing = chatMessage.getSenderId() == null || chatMessage.getSenderId().equals(currentUser.getId()); 
    setAlignment(holder, isOutgoing); 

    Collection<QBAttachment> attachments = chatMessage.getAttachments(); 
    //attachments. 
    if (attachments != null && attachments.size() > 0) { 

     String imageid = ""; 
     for (QBAttachment attachment : attachments) { 
      imageid = attachment.getId(); 
     } 

     final int imageid1 = Integer.parseInt(imageid); 

     QBContent.downloadFileTask(imageid1, new QBEntityCallbackImpl<InputStream>() { 
      @Override 
      public void onSuccess(InputStream inputS, Bundle params) { 

       if (inputS != null) { 
        Bitmap bmp = BitmapFactory.decodeStream(inputS); 
        Drawable d = new BitmapDrawable(context.getResources(), bmp); 
        if (holder.image_attachment != null) 
         holder.image_attachment.setImageDrawable(d); 
       } 
      } 
      @Override 
      public void onError(List<String> errors) { 
       Log.d("Image Download Error : ", errors.toString()); 
      } 
     }, new QBProgressCallback() { 
      @Override 
      public void onProgressUpdate(int progress) { 
      } 
     }); 
    } 
    return convertView; 
} 

的,我在這裏做什麼錯?請幫幫我 。

+0

發表您的完整的日誌 –

+0

@VivekMishra我包括日誌PLZ檢查 –

+0

'在androidclient.com.qbchatsample.ui.adapters.ChatAdapter $ BackgroundOperation.onPostExecute(ChatAdapter.java:293)'什麼是這個線?? –

回答

0

最後我得到了答案。

1。刪除AsyncTask並直接調用QBContent.downloadFileTask(...)。

2。修改了QBContent.downloadFileTask(...)。在這裏我已經在後臺線程中將InputStream解碼爲BitMap,以前我是在UI線程中做的。

QBContent.downloadFileTask(imageid1, new QBEntityCallbackImpl<InputStream>() { 
      @Override 
      public void onSuccess(final InputStream inputS, Bundle params) { 

       if (inputS != null) { 
        new Thread() { 
         public void run() { 
          final Bitmap bmp = BitmapFactory.decodeStream(inputS); 
          try { 
           context.runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             Drawable d = new BitmapDrawable(context.getResources(), bmp); 
             if (holder.image_attachment != null) 
              holder.image_attachment.setImageDrawable(d); 
            } 
           }); 
          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
         } 
        }.start(); 
       } 

      } 

      @Override 
      public void onError(List<String> errors) { 
       Log.d("Image Download Error : ", errors.toString()); 

      } 
     }, new QBProgressCallback() { 
      @Override 
      public void onProgressUpdate(int progress) { 

      } 
     }); 
+0

非常糟糕的解決方案。以正常方式開始編程並刪除StrintMode語句。 – greenapps

+0

@greenapps然後幫助我PLZ,我不知道任何其他方式。 –

+0

如果刪除StrintMode調用,會發生什麼情況? – greenapps

0

做同樣的doInBackground方法中,做一個UI線程內的行動

+0

我做了,但在那種情況下,我也得到了NetworkOnMainThreadException。 –

+0

你有沒有網絡操作?如果是的話,您在清單中添加權限。 – Sreyas

+0

其中行號碼您是例外。你可以發佈更多代碼 – Sreyas

2

試試這個

private class SampleAsync extends AsyncTask<Void, Void, Bitmap> { 

    @Override 
    protected Bitmap doInBackground(Void... params) { 
     // your background code fetch InputStream 
     InputStream s = //your inputstream ; 
     Bitmap bmp = BitmapFactory.decodeStream(s); 
     return bmp; 
    } 

    @Override 
    protected void onPostExecute(Bitmap bmp) { 
     super.onPostExecute(bmp); 
     if(bmp != null){ 
      Drawable d = new BitmapDrawable(context.getResources(), bmp); 
      if(holder.image_attachment != null) 
       holder.image_attachment.setImageDrawable(d); 
     } 
    } 
} 
+0

我做了同樣的,但行Bitmap bmp = BitmapFactory.decodeStream(s);在doInBackground()內部返回了NetworkOnMainThread異常。 –

+1

請檢查這個鏈接。 http://stackoverflow.com/questions/12615607/android-os-networkonmainthreadexception –

+0

'我做了同樣的'???你的代碼與這個處理程序和其他東西完全不同。 – greenapps

0

您是間接您呼叫的主線程網絡適配器用於更新UI和他們在主線程上運行。 要解決它或者將適配器的完整代碼移動到另一個asyncTask或從適配器中刪除您的asynctask調用。

+0

不明白這一點。請解釋爲什麼不能從getView中調用異步任務,如果這是你的話。 – greenapps

+0

你是指從getView?我之前問過這個問題。如果不是,那麼通過'從適配器調用'來解釋你的意思。 – greenapps

+0

你在getView()中調用asynctask這就是爲什麼你會得到錯誤 –

相關問題