2012-10-21 94 views
0

我製作了一個應用程序,我需要從給定的鏈接下載圖像。它顯示沒有錯誤logcat中沒有錯誤消息。以下是我的代碼在android中下載圖像

public Drawable getImage(String ImagePath,String fileName) { 

    Log.v("download", "image downloading from net"); 

    boolean bin = getBinaryWebFile(ImagePath,fileName); 
    Drawable draw = Drawable.createFromPath(SavePath+fileName); //..........(1) 
    return draw; 

}//getImage ends here*/ 

private boolean getBinaryWebFile(String inURL, String filename) { 

    File file = new File(SavePath,filename); 

    try { 

     URL url = new URL(inURL); 
     HttpURLConnection con = (HttpURLConnection)url.openConnection(); 

     con.setRequestMethod("GET"); 
     con.setDoOutput(true); 
     con.connect(); 

     InputStream is = con.getInputStream(); 
     FileOutputStream fos = new FileOutputStream(file); 
     BufferedOutputStream bout = new BufferedOutputStream(fos,1024); 
     byte[] data =new byte[1024]; 
     int x = 0; 

     while((x=is.read(data,0,1024))>=0){ 
      bout.write(data,0,x); 
     } 
     fos.flush(); 
     bout.flush(); 
     fos.close(); 
     bout.close(); 
     is.close(); 

     return true; 

    } catch (MalformedURLException e) { 
     Log.d("PowerSaver","getBinaryWebFile->MalformedURLException: "+e.getMessage()); 
     //e.printStackTrace(); 
    } catch (IOException e) { 
     Log.d("PowerSaver","getBinaryWebFile->I/O exception: "+e.getMessage()); 
    } 
    return false; 

}//getbinarywebfile ends here 

在調試一切運行良好,最後我得到繪製爲空(注1)。我甚至在清單中寫下了以下權限。

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

仍然獲得在平局null,其作爲註釋1.

回答

0

你運行模擬器內你的應用程序中引用?確保它有一個適當的互聯網連接。有時模擬器可能會使互聯網連接鬆動,尤其是當您在不重新啓動模擬器的情況下更換網絡時。

+0

燁其實我也獲取了一些數據及其未來.... – shark

1

此代碼的工作對我來說:

private static long DownloadFromUrl(Context context, URL fileUrl, 
     String fileName) throws Exception { // this is the downloader method 

    try { 

     URL myFileUrl = null; 
     myFileUrl = fileUrl; 

     // *** Internet connection 
     HttpURLConnection conn = (HttpURLConnection) myFileUrl 
       .openConnection(); 
     conn.setDoInput(true); 
     conn.connect(); 
     // int length = conn.getContentLength(); 
     // int[] bitmapData =new int[length]; 
     // byte[] bitmapData2 =new byte[length]; 

     InputStream is = conn.getInputStream(); 

     // decodificar la imagen de internet en bmImg 
     Bitmap bmImg = BitmapFactory.decodeStream(is); 

     // ***Save in the the SD 

     FileOutputStream out = new FileOutputStream(
       <path + filename>); 

     byte[] buffer = new byte[1024]; 
     int len; 
     while ((len = is.read(buffer)) > 0) { 
      out.write(buffer, 0, len); 
      tam += len; 
     } 
     is.close(); 

     // Save using the selected format and quality 
     bmImg.compress(Bitmap.CompressFormat.PNG, 
       <quality>, out); 
     out.flush(); 
     out.close(); 

     // tam=bmImg.getByteCount(); 
     bmImg.recycle(); 

     return tam; 

    } catch (IOException e) { 
     // showErrorMessage(context, "Error downloading"); 
     e.printStackTrace(); 
     return 0; 
    } 

}