2011-06-30 39 views
0

看到網絡中的所有示例後,我寫了以下代碼從URL讀取圖像,將其顯示到圖像視圖並將其保存到指定的路徑。 公共類下載{Android:從URL中讀取圖片

public void startDownload(ImageView i, String path,String url){ 

    BitmapDownloaderTask task = new BitmapDownloaderTask(i,path); 
    task.execute(url,null,null); 
} 
    static class FlushedInputStream extends FilterInputStream { 
    public FlushedInputStream(InputStream inputStream) { 
     super(inputStream); 
    } 

    @Override 
    public long skip(long n) throws IOException { 
     long totalBytesSkipped = 0L; 
     while (totalBytesSkipped < n) { 
      long bytesSkipped = in.skip(n - totalBytesSkipped); 
      if (bytesSkipped == 0L) { 
        int b = read(); 
        if (b < 0) { 
         break; // we reached EOF 
        } else { 
         bytesSkipped = 1; // we read one byte 
        } 
      } 
      totalBytesSkipped += bytesSkipped; 
     } 
     return totalBytesSkipped; 
    } 
} 
static Bitmap downloadBitmap(String url) { 
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); 
    final HttpGet getRequest = new HttpGet(url); 

    try { 
     HttpResponse response = client.execute(getRequest); 
     final int statusCode = response.getStatusLine().getStatusCode(); 
     if (statusCode != HttpStatus.SC_OK) { 
      Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
      return null; 
     } 

     final HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream inputStream = null; 
      try { 
       inputStream = entity.getContent(); 
       final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream)); 
       return bitmap; 
      } finally { 
       if (inputStream != null) { 
        inputStream.close(); 
       } 
       entity.consumeContent(); 
      } 
     } 
    } catch (Exception e) { 
     // Could provide a more explicit error message for IOException or IllegalStateException 
     getRequest.abort(); 
     System.err.println("Error while retrieving bitmap from " + url +":"+ e.toString()); 
    } finally { 
     if (client != null) { 
      client.close(); 
     } 
    } 
    return null; 
} 
private class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> { 
    private String url; 
    private final WeakReference<ImageView> imageViewReference; 
    private String path; 
    public BitmapDownloaderTask(ImageView imageView, String FilePath) { 
     imageViewReference = new WeakReference<ImageView>(imageView); 
     path = FilePath; 
    } 

    @Override 
    // Actual download method, run in the task thread 
    protected Bitmap doInBackground(String... params) { 
     // params comes from the execute() call: params[0] is the url. 
     return downloadBitmap(params[0]); 
    } 

    @Override 
    // Once the image is downloaded, associates it to the imageView 
    protected void onPostExecute(Bitmap bitmap) { 
     if (isCancelled()) { 
      bitmap = null; 
     } 

     if (imageViewReference != null) { 
      ImageView imageView = imageViewReference.get(); 
      if (imageView != null) { 
       imageView.setImageBitmap(bitmap); 
      } 
     } 
     OutputStream outStream = null; 
     File file = new File(path); 
     try { 
     outStream = new FileOutputStream(file); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
     outStream.flush(); 
     outStream.close(); 
     } 
     catch(Exception e) 
     {} 


    } 
} 

現在的問題是此代碼的工作有時有有時失敗...錯誤是

06-30 12:34:23.363: WARN/System.err(16360): Error while retrieving bitmap from https://URL IS HERE---REMOVE FOR PRIVACY:java.net.SocketTimeoutException: Read timed out 

和一段時間內得到 SkImageDecoder ::廠返回null。

幫我找到可能的原因

回答

1

考慮這個例子展示瞭如何從URL

   URL url = new URL(Your Image URL In String); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      yourImageView.setImageBitmap(myBitmap); 
+0

創建圖像得到的錯誤SkImageDecoder ::廠返回null – jhon

+0

@約翰:請確保您的網址是處於打開狀態..如果德的圖像是一個問題在URL中,位圖將爲空 – dhams

+0

問題主要發生在URL未以「.jpg」結尾的情況下。它無法下載文件atall – jhon