我正在开发Android应用程序,我正在尝试将InputStream解码为Bitmap。
Bitmap bmp = BitmapFactory.decodeStream(s);
QBContent.downloadFileTask(...)中的上一行抛出NetworkOnMainThread异常。
以下是参考代码:
ChatAdapter的getView()
@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 :(得分: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);
}
}
}
答案 1 :(得分:0)
在doInbackground方法中执行相同操作并在UI线程内执行操作
答案 2 :(得分:0)
你是间接地在主线程上调用网络,因为适配器用于更新ui并且它们在主线程上运行。 要解决此问题,请将带有适配器的完整代码移动到另一个asyncTask,或者从适配器中删除asynctask调用。
答案 3 :(得分: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) {
}
});