在Android中未显示图像。

huangapple go评论77阅读模式
英文:

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&lt;String&gt; url = new ArrayList&lt;String&gt;();
ArrayList&lt;String&gt; name = new ArrayList&lt;String&gt;();
public class DOwnloadTask extends AsyncTask&lt;String, Void, String&gt; {
@Override
protected String doInBackground(String... strings) {
URL url;
int data;
HttpURLConnection connect;
String result = &quot;&quot;;
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&lt;String, Void, Bitmap&gt; {
@Override
protected Bitmap doInBackground(String... strings) {
URL url;
int data;
HttpURLConnection connect;
String result = &quot;&quot;;
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 = &quot;&quot;;
try {
result = task.execute(&quot;http://www.posh24.se/kandisar&quot;).get();
String[] a = result.split(
&quot;&lt;div class=\&quot;listedArticles\&quot;&gt;&quot;);
Pattern pattern = Pattern.compile(&quot;alt=\&quot;(.*?)\&quot;&quot;);
Matcher m = pattern.matcher(a[0]);
while (m.find()) {
name.add(m.group(1));
}
pattern=Pattern.compile(&quot;img src=\&quot;(.*?)\&quot;&quot;);
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);

huangapple
  • 本文由 发表于 2020年4月5日 01:48:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61032286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定