Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

BitmapFactory.decodeStream throwing NetworkOnMainThreadException - Android

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this

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);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...