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

The Archive Base Latest Questions

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

I need to download a single image at time from the Internet and then

  • 0

I need to download a single image at time from the Internet and then save it on the SD card. How do I do it? I have made an attempt, but when I try to view that downloaded image, it shows the message, “No Preview Available”. Please see my code below:

public class ImgDownloader {

 private static final int IO_BUFFER_SIZE = 4 * 1024;

 public static final byte[] downloadImage(String imgURL) {
  byte[] data = null;
  try {
   Log.v("Down", "1");
   InputStream in = null;
   BufferedOutputStream out = null;
   in = new BufferedInputStream(new URL(imgURL).openStream(), 8 * 1024);
   Log.v("Down", "2");
   final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
   out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
   copy(in, out);
   out.flush();
   Log.v("Down", "3");
   data = dataStream.toByteArray();
   // bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
   Log.v("Down", "4");
  } catch (Exception ex) {
   ex.printStackTrace();
   // System.out.println("Exception in Image Downloader .."
   // +ex.getMessage());
  }
  return data;
 }

 private static void copy(InputStream in, OutputStream out)
   throws IOException {
  byte[] b = new byte[IO_BUFFER_SIZE];
  int read;
  while ((read = in.read(b)) != -1) {
   out.write(b, 0, read);
  }
 }

}

Note:
i have download the image from the SSL connection.

Any ideas? Thanks in advance.

  • 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:47:11+00:00Added an answer on May 18, 2026 at 8:47 pm

    You can try something like

    try{
            URL url = new URL(downloadUrl); //you can write here any link
            File file = new File(absolutePath); //Something like ("/sdcard/file.mp3")
    
                //Create parent directory if it doesn't exists
                if(!new File(file.getParent()).exists())
                {
                    System.out.println("Path is created " + new File(file.getParent()).mkdirs());
                }
                file = new File(absolutePath); //Something like ("/sdcard/file.mp3")
                file.createNewFile();
                /* Open a connection to that URL. */
                URLConnection ucon = url.openConnection();
                /*
                 * Define InputStreams to read from the URLConnection.
                 */
                InputStream is = ucon.getInputStream();
                /*
                 * Read bytes to the Buffer until there is nothing more to read(-1).
                 */
                FileOutputStream fos = new FileOutputStream(file);
                int size = 1024*1024;
                byte[] buf = new byte[size];
                int byteRead;
                while (((byteRead = is.read(buf)) != -1)) {
                    fos.write(buf, 0, byteRead);
                    bytesDownloaded += byteRead;
                }
                /* Convert the Bytes read to a String. */
    
                fos.close();
    
        }catch(IOException io)
        {
            networkException = true;
            continueRestore = false;
        }
        catch(Exception e)
        {   
            continueRestore = false;
            e.printStackTrace();
        }
    

    Make the appropriate changes according to your requirement. I use the same code for downloading files from internet and saving it to SDCard.

    Hope it helps !!

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

Sidebar

Related Questions

I need to download an image from some URL to my server. However, my
I need to download images from a website, and I have the login name
I have tried other methods to download info from a URL, but needed a
I am using Ubuntu and have a known URL which I need to download/save
I'm designing an app where i need to download multiple images from time to
I need to download file from FTP server and make some changes on it
I need to download one file from an url that i give from an
I need to download and install xcode . But i need to know the
I need to download two Excel files onto the client, and then run a
I have make an application. In this application client need that when user download

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.