ImageView issue (Android Studio, Java)

I need the widget’s background to be loaded from my database (either the picture itself, or the picture linked to it). After looking at many tutorials (here is an example of what I need: https://youtu.be/mOhNdP1QZ7E), I could not solve this problem. The picture is not displayed.
The java class code is 100% taken from the tutorial above. Tried others too.

code example

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class content_scrolling extends AppCompatActivity
{
 ImageView imageView;
 Bitmap myBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_scrolling);

    imageView1=(ImageView)findViewById(R.id.d1back);

    loadimage(imageView);
}

public void loadimage(View view)
{
    imageView1=(ImageView)findViewById(R.id.d1back);

     class ImageLoadTask extends AsyncTask<Void, Void, Bitmap>
     {

        private String url;
        private ImageView imageView;

        public ImageLoadTask(String url, ImageView imageView) {
            this.url = url;
            this.imageView = imageView;
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            try {
                URL connection = new URL(url);

                InputStream input = connection.openStream();
                myBitmap = BitmapFactory.decodeStream(input);
                Bitmap resized = Bitmap.createScaledBitmap(myBitmap, 1000, 400, true);
                return resized;
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            super.onPostExecute(result);
            imageView.setImageBitmap(result);
        }

    }

    ImageLoadTask obj=new ImageLoadTask("http://....png",d1back);
    obj.execute();
}

}`

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

you can simply use a library, which is Glide.

1. add a dependency to build.gradle (app)

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

2. add the following code to the java file.

String url="https://www.google.com" //add your URL here
ImageView imageView = findViewById(R.id.imageViewId);
Glide.with(this).load(url).into(imageView);

Solution 2

You can easily load any kind of image in image view using Picasso or Glide.

Using Picasso:

implementation 'com.squareup.picasso:picasso:2.71828'


Picasso.get()
    .load(imageUrl)
    .into(test_imageview)

Using Glide:

implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'


Glide.with(this)
.load(imageUrl)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(test_imageview)

Solution 3

Glide lib helps me. Thanks friends.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply