1

I'm trying to download an image using a URL and a button in my app. When I'm running it on my phone, I,m not able to download the image. Can anyone please point out the problem with this. Thanks for the help in advance :)

This is my code.

public class MainActivity extends AppCompatActivity {

ImageView download;

public void downloadImage(View view){

    DownloadImage image = new DownloadImage();
    Bitmap result;
    try {
        result = image.execute("https://en.wikipedia.org/wiki/File:Bart_Simpson_200px.png").get();
        download.setImageBitmap(result);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    download = (ImageView) findViewById(R.id.imageView);
}

public class DownloadImage extends AsyncTask<String, Void, Bitmap>{

    @Override
    protected Bitmap doInBackground(String... urls) {
        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.connect();
            InputStream in = urlConnection.getInputStream();
            Bitmap Image = BitmapFactory.decodeStream(in);
            in.close();
            return Image;
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
         return null;
    }
}
}
1
  • I would very strongly recommend that you use Glide and not an AsyncTask. I use it for all my image loading, local or remote. Commented Jun 23, 2017 at 17:46

2 Answers 2

3

you can download image from url in two ways

1.you can user Glide library to load image from url look the below code it can help you in simple way

compile this library

   implementation 'com.github.bumptech.glide:glide:4.6.1'

than load image like this

 Glide.with(MainActivity.this)
        .load(url)
        .apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
        .into(imageview);

2 .try this if you dont want to use third party library

 new DownloadImage(imamgeview).execute(url);

create a Async Task

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImage(ImageView bmImage) {
    this.bmImage = (ImageView ) bmImage;
}

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.d("Error", e.getStackTrace().toString());

    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
}
}

i hope that it will work in your case

Sign up to request clarification or add additional context in comments.

Comments

0

You did not call the function

downloadImage(view);

In the oncreate (after the findviewbyid line)

Also check if the internet permision is in the Android Manifest file :

    <uses-permission android:name="android.permission.INTERNET" />

Having said that you should use a library like Glide\Picasso\Ion etc... much better than asynctask for this purpuse

https://github.com/bumptech/glide - Glide

https://github.com/koush/ion - Ion

http://square.github.io/picasso/ - picasso

Hope it helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.