DownloadImageActivity.java
Dosyayı İndir
package com.godoro.androidclients;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownloadImageActivity extends Activity {
private TextView messageView;
private ImageView imageView;
private String imageAddress="http://www.godoro.com/images/GodoroSymbolAndNameSmall.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download_image);
messageView=(TextView) findViewById(R.id.messageView);
imageView=(ImageView) findViewById(R.id.imageView);
}
public void onClickDownloadImage(View view){
Runnable runnable=new Runnable() {
@Override
public void run() {
retrieveImage();
}
};
Thread thread=new Thread(runnable);
thread.start();
}
private void retrieveImage(){
try {
final Bitmap bitmap=downloadBitmap(imageAddress);
Runnable action=new Runnable() {
@Override
public void run() {
showImage(bitmap);
}
};
runOnUiThread(action);
}catch(Exception e){
Log.e("GodoroAndroid","İndirme yanlışı",e);
}
}
private void showImage(Bitmap bitmap){
imageView.setImageBitmap(bitmap);
messageView.setText(R.string.imageDownloaded);
}
private static Bitmap downloadBitmap(String imageAddress){
try {
URL url = new URL(imageAddress);
URLConnection conn = url.openConnection();
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
Bitmap bitmap = BitmapFactory.decodeStream(bis);
return bitmap;
}catch (Exception e){
Log.e("GodoroAndroid", "Görüntü indirme yanlışı", e);
return null;
}
}
}
Dosyayı İndir