1

I am trying to download the images from url and storing it on sd card, it works fine for .jpg file but if image extension are .png or .jpeg then it is giving error , i want to download the diferent format images at the same time ..below is code which i have typed..

 public void DownloadFromUrl(String DownloadUrl, String fileName) {

    try {
        File dir = new File("/sdcard/pluto");

        if (dir.exists() == false) {
            dir.mkdirs();
        }

        URL url = new URL(DownloadUrl); // you can write here any link
        File file = new File(dir, fileName);

        long startTime = System.currentTimeMillis();
        Log.d("DownloadManager", "download begining");
        Log.d("DownloadManager", "download url:" + url);
        Log.d("DownloadManager", "downloaded file name:" + fileName);

        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Log.d("DownloadManager",
                "download ready in"
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");

    } catch (IOException e) {
        Log.d("DownloadManager", "Error: " + e);
    }

}

please help me..

1 Answers1

0

Try this may be helpful .

same post with different image format.

Image download code works for all image format, issues with PNG format rendering

Community
  • 1
  • 1
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85