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

  • Home
  • SEARCH
  • 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 1898236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:44:27+00:00 2026-05-17T06:44:27+00:00

Can someone please tell me where I’m going wrong with this piece of code?

  • 0

Can someone please tell me where I’m going wrong with this piece of code? Basically I want the app to download a .png file, display it to the user and also save it to the sd card for future reference. It seems to be all going fine until the while ((len = webstream.read(buffer)) > 0) part; as the debugger doesn’t reach the breakpoint I’ve put in here, and I have a directory of empty .png files. I have the WRITE_EXTERNAL_STORAGE permission declared in my Manifest.

protected class DownloadTask extends AsyncTask<String, Integer, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... string) {

        Bitmap d = null;

        try {

            DefaultHttpClient dhc = new DefaultHttpClient();
            HttpGet request = new HttpGet(HTTP_BASE + string[0] + ".png");
            HttpResponse response = dhc.execute(request);
            BufferedInputStream webstream = new BufferedInputStream(response.getEntity().getContent());
            d = BitmapFactory.decodeStream(webstream);
            writeToSd(string[0], webstream, d);

        }
        catch (Exception ex) { ex.printStackTrace(); }

        return d;

    }

    private void writeToSd(String string, BufferedInputStream webstream, Bitmap d) {

        try {

            webstream.mark(3);
            webstream.reset();
            File f = new File(Environment.getExternalStorageDirectory() + SD_DIR);
            f.mkdirs();
            File f2 = new File(f, string + ".png");
            f2.createNewFile();
            BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(f2));

            int len;
            byte[] buffer = new byte[1024];
            while ((len = webstream.read(buffer)) > 0) {

                fos.write(buffer, 0, len);

            }

            webstream.close();
            //fos.flush();
            fos.close();

        }
        catch (Exception ex) {

            ex.printStackTrace();

        }


    }

    protected void onPostExecute(Bitmap result) {

        if (result != null) {
            iv.setImageBitmap(result);
            vs.setDisplayedChild(1);
        }

    }

};
  • 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-17T06:44:28+00:00Added an answer on May 17, 2026 at 6:44 am

    I don’t know for sure, but it seems likely that there is something wrong with the re-reading of the InputStream, even though it’s buffered. An image is quite a bit of data to hold in memory. Rather than doing

    Fetch Image, Display Image, Store Image

    you could try to

    Fetch Image, Store Image, Display Image

    This way you can write a full-sized image to disk (without having to buffer the stream and consume a lot of memory). Then, you can simply load the image from the sd card at the lowest sampling rate that works for your application, thus saving having a full-sized image ever in memory. Also, doing it this way would likely eliminate any issues you may or may not be having with the bufferedInputStream – I find them to be pretty finicky.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please tell me what's wrong with this code? Basically what I am
Can someone please tell me where is the error in this code?, I use
can someone please tell me why this xml formatting is incorrect and why my
Can someone please tell what this strange error is Mod_python error: PythonHandler django.core.handlers.modpython Traceback
Can someone please tell what is the code for checking the size of a
can someone please tell what I'm doing wrong here? \newcommand{\bc}{\small\begin{verbatim}} \newcommand{\ec}{\normalsize\end{verbatim}} and then \bc
Can someone please tell me how to convert this to razor view syntax? <%=
Could someone please tell me which objects types can be tested using Regular Expressions
Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can someone please tell me how to print out a variable in my XSL

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.