Reputation: 25
I've been trying to write code in which the app gets image links from a webpage, converts it into a bitmap and then displays the image.However, even though studio shows no errors, the image isn't being displayed.The code is written below.Please excuse wrong usages of jargon as I'm new to this.
package com.example.android.web;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
ArrayList<String> url = new ArrayList<String>();
ArrayList<String> name = new ArrayList<String>();
public class DOwnloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
URL url;
int data;
HttpURLConnection connect;
String result = "";
try {
url = new URL(strings[0]);
connect = (HttpURLConnection) url.openConnection();
InputStream in = connect.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
data = reader.read();
while (data != -1) {
char curr = (char) data;
result += curr;
data = reader.read();
}
return result;
} catch (Exception e) {
return null;
}
}
}
public class Imagedownloader extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... strings) {
URL url;
int data;
HttpURLConnection connect;
String result = "";
try {
url = new URL(strings[0]);
connect = (HttpURLConnection) url.openConnection();
connect.connect();
InputStream in = connect.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
return bitmap;
} catch (Exception e) {
return null;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = findViewById(R.id.imageView);
DOwnloadTask task = new DOwnloadTask();
String result = "";
try {
result = task.execute("http://www.posh24.se/kandisar").get();
String[] a = result.split(
"<div class=\"listedArticles\">");
Pattern pattern = Pattern.compile("alt=\"(.*?)\"");
Matcher m = pattern.matcher(a[0]);
while (m.find()) {
name.add(m.group(1));
}
pattern=Pattern.compile("img src=\"(.*?)\"");
m=pattern.matcher(a[0]);
while(m.find()){
url.add(m.group(1));
}
Random r=new Random();
int chosen=r.nextInt(url.size());
Imagedownloader downloader=new Imagedownloader();
Bitmap bit;
bit=downloader.execute(url.get(chosen)).get();
img.setImageBitmap(bit);
} catch (Exception e) {
}
Any help in identifying where the problem is would be appreciated as the imageview is empty during runtime
Upvotes: 1
Views: 65
Reputation: 384
Use Glide library for displaying the image. https://github.com/bumptech/glide
To insert the image from the URL into your imageView use:
Glide.with(this).load(url).into(img);
Upvotes: 2