2017-03-15 69 views
-1

我試圖從互聯網下載圖像。目前,該應用只下載圖像的一部分,但隨後停止。我正在使用AsyncTask。有什麼建議做什麼?使用Android從服務器下載圖像

我得到的錯誤是:

android.os.NetworkOnMainThreadException 
at com.example.reynaldo.getimageserver.MainActivity$DownloadAsyncTask.onPostExecute(MainActivity.java:74) 
W/System.err:  at com.example.reynaldo.getimageserver.MainActivity$DownloadAsyncTask.onPostExecute(MainActivity.java:30) 

這是我的代碼有:

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.ImageView; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

public void onClick (View view){ 
    DownloadAsyncTask task = new DownloadAsyncTask(); 
    task.execute(new String[] {"http://res.cloudinary.com/demo/image/upload/w_500,f_auto/sample.jpg" }); 
} 

private class DownloadAsyncTask extends AsyncTask<String, Void, String> { 


    Bitmap bitmap = null; 
    InputStream in = null; 

    @Override 
    protected String doInBackground(String... urls){ 


     int response = -1; 
     URL url; 

     try { 
      url = new URL("http://res.cloudinary.com/demo/image/upload/w_500,f_auto/sample.jpg"); 

     URLConnection conn = url.openConnection(); 

     if (!(conn instanceof HttpURLConnection)) 
      System.out.println("This is wrong"); 


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

    catch (IOException e) { 
     System.out.println("IOException"); 
    } 
     return "Download failed"; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     //try { 
      bitmap = BitmapFactory.decodeStream(in); 
     /* in.close(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     }*/ 
     ImageView img = (ImageView) findViewById(R.id.image); 

     img.setImageBitmap(bitmap); 
    } 

} 

}

+0

一個問題是的AsyncTask的onPostExecute()在UI線程上運行,你不能做的UI線程上網絡。您可以在doInBackground()內部解碼位圖,然後讓onPostExecute()將其提供給View。 – Code4aliving

+0

或者你可以像畢加索 – theblitz

+0

一樣的圖像下載使用畢加索或翻新圖像下載。 – HaroldSer

回答

0

試試這個,如果你不想使用第三方庫

 new DownloadImage(Imageview).execute(YourUrl); 

創建的AsyncTask類

public class DownloadImage extends AsyncTask<String, Void, Bitmap> { 
    CircleImageView bmImage; 

    public DownloadImage(ImageView bmImage) { 
     this.bmImage = (CircleImageView) bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.d("Error", e.getStackTrace().toString()); 

     } 
     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
    } 
} 
相關問題