java – Loading png via url into an existing imageview

Question:

You just need to load the png image via the url into the existing imageview .

I tried, as they said here . .jpg pictures perfectly load and display, but does not want to display the .png format, it turns out a blank screen (tested on versions 4.1 and 4.2).

Why is this happening and how to fix it?

if (isOnline()) {
        new DownloadImageTask((ImageView) findViewById(R.id.imageViewSA))
                .execute(img);
    } else {
        tv_info.setText("Включите интернет для загрузки");
    }

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = 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.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

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

    }
}

Answer:

Use Picasso , it is the most simple, code-efficient and stable.

In the dependencies section (build.gradle) add the library

compile 'com.squareup.picasso:picasso:2.5.2'

Then, in your code, import:

import com.squareup.picasso.Picasso;

and use

ImageView shopLogo = (ImageView) findViewById(R.id.imageViewLogo);
Picasso.with(this).load(logoURL).into(shopLogo);

For the picture, by default, there is .placeholder, and for the picture, if there is a loading error, there is .error

Picasso
.with(this)
.load(logoURL)
.placeholder(getApplicationContext().getResources().getDrawable(R.drawable.default_photo))
.error(getApplicationContext().getResources().getDrawable(R.drawable.default_photo))
.into(shopLogo);
Scroll to Top