Well I am a begginer and I would be happy for some help.
So when you press the button,the Image should load from the website the image and show it as an
ImageView.
I try to run this code,but everytime I press the button to show the image,the app crash.
Thank you:)
public class MainActivity extends AppCompatActivity {
ImageView mImageView;
public void clickButton(View view){
Log.d("Proccess" , "Button pressed");
ImageDownloader myTask = new ImageDownloader();
Bitmap myImage;
try {
myImage = myTask.execute("https://ichef.bbci.co.uk/news/410/media/images/47104000/jpg/_47104670_homer_226other.jpg").get();
mImageView.setImageBitmap(myImage);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = findViewById(R.id.mImageView);
}
public class ImageDownloader extends AsyncTask< String,Void, Bitmap > {
@Override
protected Bitmap doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream myStream = connection.getInputStream();
Bitmap in = BitmapFactory.decodeStream(myStream);
return in;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}