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 3241742
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:14:03+00:00 2026-05-17T18:14:03+00:00

I created a separate project and activity that downloads an image from a URL,

  • 0

I created a separate project and activity that downloads an image from a URL, converts it to a Bitmap, and then uses a FileOutputStream to save that file to the SD card. When in the separate project and free-standing activity, this worked fine, and I could see that the image is stored on the SD card.

public class PictureDownload extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        downloadFromUrl("http://www.coutblog.com/prism/uploads/0030.jpg", "newpic.jpg");
        displayPic("newpic.jpg");
    }

    // Place to download the file
    private final String PATH = "/sdcard/";

    public void displayPic(String filename) {
        ImageView image = (ImageView)findViewById(R.id.image);
        Bitmap bMap = BitmapFactory.decodeFile(PATH + filename);
        image.setImageBitmap(bMap);
    }

    /**
     * Downloads the picture from the URL to the SD card
     * @param imageURL: the URL to download it from (absolute web URL)
     * @param fileName: the name of the file you want to save the picture to
     */
    public void downloadFromUrl(String imageURL, String fileName) {
        try {
                // Connect to the URL
                URL myImageURL = new URL(imageURL);
                HttpURLConnection connection = (HttpURLConnection)myImageURL.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();

                // Get the bitmap
                Bitmap myBitmap = BitmapFactory.decodeStream(input);

                // Save the bitmap to the file
                String path = Environment.getExternalStorageDirectory().toString();
                OutputStream fOut = null;
                File file = new File(path, fileName);
                fOut = new FileOutputStream(file);

                myBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                fOut.flush();
                fOut.close();
            } catch (IOException e) {}
    }

}

But, whenever I tried to port the code to an existing project, it fails. In this setting, I am running the code from a service. The code is the exact same – but for some reason it won’t download any files. Would this have to do with the fact that it is running on a service instead of an activity? Or maybe for some reason the file output stream gets messed up.

In BGService:

String name = remote_resource.getValue().substring(38);
                downloadFromUrl(remote_resource.getValue(), name);
 /**
 * Downloads the picture from the URL to the SD card
 * @param imageURL: the URL to download it from (absolute web URL)
 * @param fileName: the name of the file you want to save the picture to
 */
public void downloadFromUrl(String imageURL, String fileName) {
    try {
            // Connect to the URL
            URL myImageURL = new URL(imageURL);
            HttpURLConnection connection = (HttpURLConnection)myImageURL.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();

            // Get the bitmap
            Bitmap myBitmap = BitmapFactory.decodeStream(input);

            // Save the bitmap to the file
            String path = Environment.getExternalStorageDirectory().toString();

            OutputStream fOut = null;
            File file = new File(path, fileName);
            Log.i("help", file.getAbsolutePath());
            Log.i("help", file.toString());
            Log.i("help", file.length() + "");
            System.out.println("THIS IS A TEST OF SYSTEM.OUT.PRINTLN()");
            fOut = new FileOutputStream(file);

            myBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
            fOut.flush();
            fOut.close();
            System.out.println("file Path: " + file.getAbsolutePath());
            Log.i("help2", file.getAbsolutePath());
            Log.i("help2", file.toString());
            Log.i("help2", file.length() + "");

        } catch (IOException e) {}
}

Another interesting thing is that of the “Log.i” calls below, only the second one (below the fOut.flush()) gets printed. So somewhere in the bitmap compression or the FileOutputStream IO something weird is happening. But no exceptions are thrown.

Thanks a lot for your 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-17T18:14:03+00:00Added an answer on May 17, 2026 at 6:14 pm

    Are you sure you actually have the WRITE_EXTERNAL_STORAGE is enabled. If you are using the permission-group.STORAGE it doesn’t always include this in there.

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

Sidebar

Related Questions

I have an Android project completely fine, then I created a separate workspace and
I created a separate assembly to contain common extension methods, the extension methods uses
My project has an Activity Monitor that monitors the activity of a user. Currently
I created a separate web service project using VS 2005. My first question is
I've created a UserControl in a separate project in the same solution I want
I am wondering how to use NUnit correctly. First, I created a separate test
I have written a Windows service that spawns a separate process. This process creates
I hope this makes sense. I have a ASP.NET web application that uses Entity
I have created an EJB3 project and a JPA project. I'm trying to create
folks. I've created a setup project in VS2008 which installs fine by itself. However

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.