Android URL Bitmap Kullanımı 2

activity_main.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<ImageView
android:id=”@+id/imageView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
</RelativeLayout>

MainActivity.java
public class MainActivity extends AppCompatActivity {

ImageView img;
Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.imageView);
new LoadImage().execute(“PHOTO_URL”);
}

private class LoadImage extends AsyncTask<String, String, Bitmap> {
@Override
protected Bitmap doInBackground(String… args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
img.setImageBitmap(image);
}
}
}
}

AndroidManifest.xml içerisine;
<uses-permission android:name=”android.permission.INTERNET” />