1

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

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
  • Your networking code may be crashing with a `NetworkOnMainThreadException`, from your `get()` calls on `AsyncTask`. More importantly, *never* catch an `Exception` without logging it. In your `catch` block, add `Log.e("AkulApp", "Exception doing network I/O", e);`. Then, [use Logcat to examine the Java stack trace](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). All of this should be demonstrated in whatever book on Android app development you read. – CommonsWare Apr 04 '20 at 17:53
  • I logged it as you suggested and this is what I got Exception doing network I/O java.io.IOException: Cleartext HTTP traffic to www.posh24.se not permitted Exception doing network I/O java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference – Akul Shivakumar Apr 04 '20 at 18:27
  • Use `https` URLs. – CommonsWare Apr 04 '20 at 18:32
  • 1
    What does it have to do with Android Studio? – Dharman Apr 04 '20 at 21:48

1 Answers1

2

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);
mayur
  • 374
  • 1
  • 10