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.
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();
}
}`
CodePudding user response:
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);