Skip to content

Instantly share code, notes, and snippets.

@ishitcno1
Created January 1, 2014 08:46
Show Gist options
  • Save ishitcno1/8206279 to your computer and use it in GitHub Desktop.
Save ishitcno1/8206279 to your computer and use it in GitHub Desktop.
Android download and show an image. Show how to use HttpURLConnection and AsyncTask.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showPicture"
android:text="Show Picture" />
<ImageView
android:id="@+id/main_iv_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edinstudio.app.training"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.edinstudio.app.training.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.edinstudio.app.training;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity {
public static final String TAG = "training";
private ImageView mIvPicture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIvPicture = (ImageView) findViewById(R.id.main_iv_picture);
}
public void showPicture(View view) {
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
new DownloadImageAsyncTask()
.execute("http://developer.android.com/design/media/principles_delight.png");
} else {
Toast.makeText(this, "No network connection available.", Toast.LENGTH_SHORT).show();
}
}
public static Bitmap downloadImageFromUrl(String s) {
Bitmap bitmap = null;
try {
URL url = new URL(s);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int responseCode = conn.getResponseCode();
Log.e(TAG, "Response code is " + responseCode);
bitmap = BitmapFactory.decodeStream(conn.getInputStream());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return bitmap;
}
private class DownloadImageAsyncTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... strings) {
return downloadImageFromUrl(strings[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
mIvPicture.setImageBitmap(bitmap);
} else {
Log.e(TAG, "In onPostExecute bitmap is null");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment