I am working on an Android App where I am trying to decode InputStream to Bitmap .
Bitmap bmp = BitmapFactory.decodeStream(s);
Above line inside QBContent.downloadFileTask(...) throwing NetworkOnMainThread exception.
Here is the reference code :
getView() of 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;
}
What wrong I am doing here ? Help me please .
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…