Skip to content

Instantly share code, notes, and snippets.

@josejuansanchez
Created December 1, 2015 15:49
Show Gist options
  • Save josejuansanchez/cde16539f52764c0326a to your computer and use it in GitHub Desktop.
Save josejuansanchez/cde16539f52764c0326a to your computer and use it in GitHub Desktop.
Ejemplo 01 - AsyncTask
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.josejuansanchez.ejemploasynctask.MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load image"
android:layout_weight="1" />
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="2" />
</LinearLayout>
Recuerda incluir este permiso
<uses-permission android:name="android.permission.INTERNET" />
public class ImageBackgroundTask extends AsyncTask<String, Void, Bitmap> {
Activity mActivity;
public ImageBackgroundTask(Activity activity) {
mActivity = activity;
}
@Override
protected Bitmap doInBackground(String... params) {
return downloadImage(params[0]);
}
private Bitmap downloadImage(String urlString) {
URL url;
try {
url = new URL(urlString);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
InputStream is = httpCon.getInputStream();
int fileLength = httpCon.getContentLength();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead, totalBytesRead = 0;
byte[] data = new byte[2048];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
totalBytesRead += nRead;
}
byte[] image = buffer.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
Log.i("ImageBackgroundTask", "onPostExecute Called");
if (result != null) {
((ImageView) mActivity.findViewById(R.id.imageview)).setImageBitmap(result);
}
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button mButton;
TextView mTextView;
String mUrl[] = {
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Android_robot.svg/511px-Android_robot.svg.png",
"http://innatia.info/images/galeria/coco-0.jpg",
"http://cuantas-calorias.org/wp-content/uploads/2015/01/calorias-de-la-naranja.jpg"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mTextView = (TextView) findViewById(R.id.textview);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Random ran = new Random();
int index = ran.nextInt(3);
new ImageBackgroundTask(this).execute(mUrl[index]);
}
public void setText(String newText) {
mTextView.setText(newText);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment