Possible Duplicate:
Download a file with Android, and showing the progress in a ProgressDialog
I am trying to load a image from a website,I saw this question : Android load from URL to Bitmap but my application crash at this line :
conn.connect();
public class HTTPTest extends Activity {
ImageView imView;
String imageUrl = "http://api.androidhive.info/images/sample.jpg";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button bt3 = (Button) findViewById(R.id.get_imagebt);
bt3.setOnClickListener(getImgListener);
imView = (ImageView) findViewById(R.id.imview);
}
View.OnClickListener getImgListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
downloadFile(imageUrl);
}
};
Bitmap bmImg;
void downloadFile(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, HTTPImage load test"
/>
<Button
android:id="@+id/get_imagebt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get an image"
android:layout_gravity="center"
/>
<ImageView
android:id="@+id/imview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
My manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidtest.HTTPTest"
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>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!-- Permission to write to external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
Thanks in advance for you help!
Germain.
As of Android 3+ you cannot perform potentially slow network operations of the main thread.
Download the image in an AsyncTask so that it won’t slow down the user’s experience. Simply move your
downloadFile()method into thedoInBackground()method of an AsyncTask.There is a very extensive answer here if you need detailed specifics.
Addition
Using Jimbo’s
doInBackground()you are probably getting an error from attempting to access the UI from a different thread, which you cannot do. You can overrideonPostExecute()in your AsyncTask to work with your Views since it has access to the UI thread: