Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3597930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:10:47+00:00 2026-05-18T20:10:47+00:00

What the application does is very simple. It loads two RSS feeds and on

  • 0

What the application does is very simple. It loads two RSS feeds and on each RSS feed, it contains two items with a title, an image and a link. And then it downloads the images, saves it on to local folder and display them. So two RSS feeds, and 4 images downloading.

I use AsyncTask to do this. So, two AsyncTasks objects are called to load two RSS feeds and 4 AsyncTasks to load 4 images. The problems happens when it tries to download images. For the first run, it works fine. but if I keep reloading them, sometimes AsyncTask doesn’t do anything. And sometimes it fails to read inputstream… very weird.

Is there any rule that I am missing to use AsyncTask?

Below is the snippets of my code.

public class TunesAppsWidgetProvider extends AppWidgetProvider {

private Intent taService = null;
private static boolean widgetEnabled = false;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    taService = new Intent(context, UpdateService.class);
    context.startService(taService);
}

public static class UpdateService extends Service {


    @Override
    public void onStart(Intent intent, int startId) {
        Log.d(Constants.TAG, "Service: onStart()");
        Context context = getApplicationContext();
        mViews = new RemoteViews(context.getPackageName(), R.layout.tunesappswidget);

        thisWidget = new ComponentName(this, TunesAppsWidgetProvider.class);
        manager = AppWidgetManager.getInstance(this);

        buildFeedUpdate();

        this.stopSelf();
        Log.d(Constants.TAG, "Stop Service");
    }

    public void buildFeedUpdate() {
        Log.d(Constants.TAG, "buildFeedUpdate");
        Context context = getApplicationContext();

        feedParser_0 = new FeedParser(context, Constants.FEED_ID_0);
        feedParser_1 = new FeedParser(context, Constants.FEED_ID_1);

        feedTask_0 = new LoadFeedTask();
        feedTask_0.execute(
                new LoadFeedTask.Payload(
                        feedParser_0,
                        UpdateService.this
                )
        );

        feedTask_1 = new LoadFeedTask();
        feedTask_1.execute(
                new LoadFeedTask.Payload(
                        feedParser_1,
                        UpdateService.this
                )
        );
    }

    public void buildCoverUpdate(byte feedID, List<Message> feedRSS, String operator) {
        Context context = getApplicationContext();

        Log.d(Constants.TAG, "buildCoverUpdate: " + feedID);
        try {
            switch(feedID) {
                case Constants.FEED_ID_0:
                        coverImage_0 = new CoverImage(context, feedRSS.get(Constants.ITEM_IDX_0).getImageLink(), Constants.SLOT_0);

            coverImageTask_0 = new LoadCoverImageTask();
                        coverImageTask_0.execute(
                            new LoadCoverImageTask.Payload(
                                    coverImage_0,
                                    UpdateService.this
                            )
                        );

                        coverImage_1 = new CoverImage(context, feedRSS.get(Constants.ITEM_IDX_1).getImageLink(), Constants.SLOT_1);

                        coverImageTask_1 = new LoadCoverImageTask();
                            coverImageTask_1.execute(
                                new LoadCoverImageTask.Payload(
                                    coverImage_1,
                                    UpdateService.this
                                )
                        );
                    break;

                case Constants.FEED_ID_1:
                        coverImage_2 = new CoverImage(context, feedRSS.get(Constants.ITEM_IDX_0).getImageLink(), Constants.SLOT_2, operator);
                    coverImageTask_2 = new LoadCoverImageTask();
                        coverImageTask_2.execute(
                                new LoadCoverImageTask.Payload(
                                        coverImage_2,
                                        UpdateService.this
                                )
                        );

                        coverImage_3 = new CoverImage(context, feedRSS.get(Constants.ITEM_IDX_1).getImageLink(), Constants.SLOT_3, operator);
                        bm_3 = coverImage_3.getDefaultCoverImage();
                            coverImageTask_3 = new LoadCoverImageTask();
                            coverImageTask_3.execute(
                                new LoadCoverImageTask.Payload(
                                    coverImage_3,
                                    UpdateService.this
                                )
                        );
                    break;

                default:
                    break;
            }
        }
        catch(Exception e) {
            Log.d(Constants.TAG, "buildCoverUpdate: " + e.toString());
        }
    }

}

And below code is for loading image. Sometimes this doInBackground never being called even though ‘execute’ is called. Sometimes it’s stuck on ‘Bitmap bitmap = BitmapFactory.decodeStream(in)’ here and never escape.

    protected LoadCoverImageTask.Payload doInBackground(LoadCoverImageTask.Payload... param) {
    HttpURLConnection httpConn = null;
    InputStream in;
    File imageFile;
    FileOutputStream fos;
    int slotIdx = param[0].coverImage.getSlotIndex();
    System.setProperty("http.keepAlive", "false");

    try{
        URL fileURL = new URL(param[0].coverImage.getFileURL());
        httpConn = (HttpURLConnection) fileURL.openConnection();
        Log.d(Constants.TAG , "openConn: Slot " + slotIdx);

        httpConn.setConnectTimeout(10000);

        if(httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            Log.d(Constants.TAG , "200OK: Slot " + slotIdx + ": " + fileURL.toString());

            in = httpConn.getInputStream();
            imageFile = new File(param[0].coverImage.getFullFilePath());
            fos = new FileOutputStream(imageFile);

            try {
                Bitmap bitmap = BitmapFactory.decodeStream(in);
                Log.d(Constants.TAG , "200OK: Slot " + slotIdx + " decodeStream(in);");
                if(bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos) == true) {
                    fos.flush();
                    fos.close();
                    in.close();
                    Log.d(Constants.TAG , "200OK-decodeOK: Slot " + slotIdx);
                    param[0].result = Constants.TRUE;
                }
                else {
                    fos.flush();
                    fos.close();
                    in.close();
                    Log.d(Constants.TAG , "200OK-decodeFail: Slot " + slotIdx);
                    param[0].result = Constants.FALSE;
                }
            }
            catch(NullPointerException e) {
                Log.d(Constants.TAG , "Slot " + slotIdx + ":" + e.toString());
                param[0].result = Constants.RETRY;
            }
        }
        else {
            Log.d(Constants.TAG , "Slot " + slotIdx + ":" + httpConn.getResponseCode());
            param[0].result = Constants.FALSE;
        }

    } catch(SocketTimeoutException e) {
        Log.d(Constants.TAG , "Time Out: " + slotIdx + ":" + e.toString());
    } catch(Exception e) {
        Log.d(Constants.TAG , "Slot " + slotIdx + ":" + e.toString());
        param[0].result = Constants.FALSE;
    }
    finally {
        if(httpConn != null) httpConn.disconnect();
        Log.d(Constants.TAG , "Slot " + slotIdx + ":httpConn.disconnect");
    }
    return param[0];
}

I am stuck on this for a couple of days.. please help…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:10:48+00:00Added an answer on May 18, 2026 at 8:10 pm

    Maybe the problem is not in your code. There is a bug in BitmapFactory.decodeStream, and if you use InputStream from http connection, it sometimes doesn’t load the image well.

    Look at http://code.google.com/p/android/issues/detail?id=6066 for more information

    Simple solution that works for me is downloading a whole content data into buffer and then
    using BitmapFactory.decodeByteArray method:

    InputStream is = httpCon.getInputStream();
    byte [] content = inputStreamToByteArray2(is);
    image = BitmapFactory.decodeByteArray(content, 0, content.length);
    
    
    public static final byte[] inputStreamToByteArray(InputStream is) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        int result = bis.read();
        while(result !=-1) {
            byte b = (byte)result;
            buf.write(b);
            result = bis.read();
        }
        return buf.toByteArray();
    }
    

    or if you don’t want to read by 1 byte:

    public static final byte[] inputStreamToByteArray(InputStream is) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        byte[] buffer = new byte[256];
        int result = bis.read(buffer);
        while(result > 0) {
            buf.write(buffer, 0, result);
            result = bis.read(buffer);
        }
        return buf.toByteArray();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What the application does is very simple. It loads two RSS feeds and on
I have this scala application which is very simple . All that it does
So I took a specific hot-spot from a very large web application, that does
I'm writing simple GUI using wxPyhon and faced some problems. My application does simple
I have an ASP.Net 4.0 web application which very frequently loads data from the
I have a very simple application which consists of an ASP.NET front end site,
I built a very simple MVC3 application to do a little demo, but I'm
I have setup a very simple test application to try RESTeasy on Jboss AS
I am working on a very simple application, using MVC2 Preview 1. I have
I hae a very simple application. I create bubbles at the bottom of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.