2013-04-01 140 views
1

我想從reome URL下載圖像,我得到SSL錯誤如何從url下載圖片?

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()); 

錯誤:

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

這是在2.3的工作? –

+1

該類型的3000000000問題。你甚至在這裏搜索?檢查右邊的相關問題 - > – WarrenFaith

回答

0

在我的項目我下載和存儲圖像的SD卡使用InputStreams以下列方式:

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(); 
} 

由於@NullPointer指出,不要忘記檢查清單文件:

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

你的問題是沒有意義的,因爲我們並不知道DownloadAndReadImage類,當我想你需要在你的清單中添加這兩種權限的方式:

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

PS。如果您正在尋找一個偉大的ImageLoder庫,我建議你的Android通用圖像裝載機:

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

+1

@SotiriosDelimanolis也許最棒的是一個大詞,但我已經在幾個項目中添加了這個庫,它的效果很好。 – NullPointer

+1

@SotiriosDelimanolis好的。 – NullPointer