daily pastebin goal
38%
SHARE
TWEET

AsyncTask

a guest Feb 3rd, 2017 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package br.exemplothreadprincipal;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.os.AsyncTask;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.ProgressBar;
  13.  
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.net.HttpURLConnection;
  17. import java.net.MalformedURLException;
  18. import java.net.URL;
  19.  
  20. public class MainActivity extends Activity {
  21. public ImageView iv;
  22.     public ProgressBar progressBar;
  23.     public Button button;
  24.  
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30.         button = (Button) findViewById(R.id.botao);
  31.         iv = (ImageView) findViewById(R.id.imagem);
  32.         progressBar = (ProgressBar)findViewById(R.id.progressBar);
  33.     }
  34.  
  35.     public void disableprogressbar(){
  36.         progressBar.setVisibility(View.GONE);
  37.     }
  38.  
  39.     public void setView(Bitmap bitmap){
  40.         iv.setImageBitmap(bitmap);
  41.     }
  42.  
  43.     public void baixarImagemWeb(View view) {
  44.         Download download = new Download();
  45.         download.execute();
  46.     }
  47.     private class Download extends AsyncTask<Void, Void, Bitmap> {
  48.  
  49.         @Override
  50.         protected Bitmap doInBackground(Void... params) {
  51.             Bitmap imagem = null;
  52.             try {
  53.  
  54.                 URL url = new URL("http://www.thiengo.com.br/img/system/logo/thiengo-80-80.png ");
  55.                 HttpURLConnection connection;
  56.                 connection = (HttpURLConnection) url.openConnection();
  57.                 connection.setDoInput(true);
  58.                 connection.connect();
  59.                 InputStream input = connection.getInputStream();
  60.                 final Bitmap imagem = BitmapFactory.decodeStream(input);
  61.  
  62.                 Log.i("livro", "baixou imagem.");
  63.             }
  64.             catch (MalformedURLException e) {
  65.                 e.printStackTrace();
  66.             }
  67.             catch (IOException e) {
  68.                 e.printStackTrace();
  69.             }
  70.             return imagem;
  71.         }
  72.  
  73.         @Override
  74.         protected void onPostExecute(Bitmap imagem) {
  75.             super.onPostExecute( imagem );
  76.             disableprogressbar();
  77.             setView( imagem );
  78.         }
  79.     }
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top