2012-12-05 30 views
-1

這是一個簡單的加載圖像功能。它在Android 2.2上運行良好,但它不下載並顯示任何圖像的android 4。 這是創建代碼:爲什麼Android 4沒有下載並顯示來自url的圖像

Bitmap bitmap = DownloadImage(weather.icon); 
holder.imgIcon.setImageBitmap(bitmap); 

持有人工作正常/ 這是功能:

private InputStream OpenHttpConnection(String urlString) throws IOException { 
    InputStream in = null; 
    int response = -1; 

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection(); 

    if (!(conn instanceof HttpURLConnection)) 
     throw new IOException("Not an HTTP connection"); 

    try { 
     HttpURLConnection httpConn = (HttpURLConnection) conn; 
     httpConn.setAllowUserInteraction(false); 
     httpConn.setInstanceFollowRedirects(true); 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 
     response = httpConn.getResponseCode(); 
     if (response == HttpURLConnection.HTTP_OK) { 
      in = httpConn.getInputStream(); 
     } 
    } catch (Exception ex) { 
     throw new IOException("Error connecting"); 
    } 
    return in; 
} 

private Bitmap DownloadImage(String URL) { 
    Bitmap bitmap = null; 
    InputStream in = null; 

    try { 
     in = OpenHttpConnection(URL); 
     BufferedInputStream bis = new BufferedInputStream(in, 8190); 

     ByteArrayBuffer baf = new ByteArrayBuffer(50); 
     int current = 0; 
     while ((current = bis.read()) != -1) { 
      baf.append((byte) current); 
     } 
     byte[] imageData = baf.toByteArray(); 
     bitmap = BitmapFactory.decodeByteArray(imageData, 0, 
       imageData.length); 
     in.close(); 
    } catch (IOException e1) { 

     e1.printStackTrace(); 
    } 
    return bitmap; 
} 

任何人都可以幫助我,這是把我累壞。 謝謝

回答

1

你正在運行Network Rquest在主UI thread

android> = 3.0不允許在主UI線程上運行Network Request。您需要使用

AsyncTask來做網絡操作。在你的類(下載圖片,你的情況)

0

地址:

在Android蜂窩StrictMode啓用時,將其關閉。

使用Async Task來執行網絡操作。

OR

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 

閱讀:
https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

+0

是的,你可以通過這種方式修復它,但這只是禁用限制。你應該修復這個問題:在UI線程上下載。 – RvdK

0
private class DownloadFile extends AsyncTask<String, Integer, String> { 
ProgressDialog mProgressDialog; 

@Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Create progress dialog 
     mProgressDialog = new ProgressDialog(Your Activity.this); 
     // Set your progress dialog Title 
     mProgressDialog.setTitle("title"); 
     // Set your progress dialog Message 
     mProgressDialog.setMessage(" message"); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.setMax(100); 
     mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     // Show progress dialog 
     mProgressDialog.show(); 
    } 

@Override 
protected String doInBackground(String... Url) { 
    String filename = "image.jpg"; 
    try { 
     URL url = new URL(mStrings[curruntPosition]); 
     URLConnection connection = url.openConnection(); 
     connection.connect(); 

     // Detect the file length 
     int fileLength = connection.getContentLength(); 

     // Locate storage location 
     String filepath = Environment.getExternalStorageDirectory() 
       .getPath(); 

     // Download the file 
     InputStream input = new BufferedInputStream(url.openStream()); 

     // Save the downloaded file 
     OutputStream output = new FileOutputStream(filepath + "/" 
       + filename); 

     // String fileName = "picss.jpg"; 

     byte data[] = new byte[1024]; 
     long total = 0; 
     int count; 
     while ((count = input.read(data)) != -1) { 
      total += count; 
      // Publish the progress 
      publishProgress((int) (total * 100/fileLength)); 
      output.write(data, 0, count); 
     } 

     // Close connection 
     output.flush(); 
     output.close(); 
     input.close(); 

    } catch (Exception e) { 
     // Error Log 
     Log.e("Error", e.getMessage()); 
     e.printStackTrace(); 
    } 

    return null; 
} 

@Override 
protected void onProgressUpdate(Integer... progress) { 
    super.onProgressUpdate(progress); 
    // Update the progress dialog 
    mProgressDialog.setProgress(progress[0]); 
    // Dismiss the progress dialog 
    mProgressDialog.dismiss(); 

} 

}

然後把下面的代碼在活動的下載按鈕。

new DownloadFile().execute(your url); 
相關問題