Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...