saravanan
saravanan

Reputation: 1092

How to download images from url?

I want to download the image from reome URL and I get SSL error

 String imgURL="http://whootin.s3.amazonaws.com/uploads/upload/0/0/23/82/Note_03_26_2013_01_10_55_68.jpg?AWSAccessKeyId=AKIAJF5QHW2P5ZLAGVDQ&Signature=Za4yG0YKS4%2FgoxSidFsZaAA8vWQ%3D&Expires=1364888750";

   final ImageView  ivCurrent;
   ivCurrent = (ImageView)findViewById(R.id.imageView1);

  // calling DownloadAndReadImage class to load and save image in sd card

     DownloadAndReadImage dImage= new DownloadAndReadImage(imgURL,1);

     ivCurrent.setImageBitmap(dImage.getBitmapImage());

The error:

javax.net.ssl.SSLException: Read error: ssl=0x19a4a0: I/O error during system call, Connection reset by peer

Upvotes: 1

Views: 438

Answers (3)

Ali
Ali

Reputation: 2042

Your question make no sense because we know nothing about DownloadAndReadImage class, By the way I think you need to add these two permissions in your manifest:

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

P.S If you are looking for a great ImageLoder library, I suggest you Android Universal Image Loader:

https://github.com/nostra13/Android-Universal-Image-Loader

Upvotes: 1

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

You are Connect to an HTTPS/HTTP URL via and the SSL certificate provided by the site is not trusted by the devise you are running the code on.

setting up trust in the Apache HTTP Client.

Upvotes: 0

RMalke
RMalke

Reputation: 4094

In my project I download and store images in SD card using InputStreams in the following way:

URL url = new URL(imageUrl);

InputStream input = url.openStream();

try {

    // The sdcard directory e.g. '/sdcard' can be used directly, or
    // more safely abstracted with getExternalStorageDirectory()
    String storagePath = Environment.getExternalStorageDirectory()
                                    .getAbsolutePath();

    int barIndex = imageUrl.indexOf("/");
    String path = imageUrl.substring(barIndex + 1) + ".jpg";

    String sdcardPath = storagePath + "/myapp/";

    File sdcardPathDir = new File(sdcardPath);

    sdcardPathDir.mkdirs();

    OutputStream output = new FileOutputStream(sdcardPath + imagemId + ".jpg");

    try {
        byte[] buffer = new byte[4 * 1024];
        int bytesRead = 0;
        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
            output.write(buffer, 0, bytesRead);
        }
    } finally {
        output.close();
    }

} finally {
    input.close();
}

As @NullPointer pointed out, don't forget to check the manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 0

Related Questions