英文:
Image not displayed in Android
问题
我一直在尝试编写一个应用程序,该应用程序从网页获取图像链接,将其转换为位图,然后显示图像。然而,尽管工作室没有显示错误,但图像未显示出来。以下是代码。请原谅术语的错误用法,因为我对此还很陌生。
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) {
}
}
}
任何有关问题所在的帮助都将不胜感激,因为在运行时ImageView是空的。
英文:
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
答案1
得分: 2
使用Glide库来显示图片。https://github.com/bumptech/glide
要从URL插入图片到你的imageView中,请使用:
Glide.with(this).load(url).into(img);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论