0

I'm working on a new Android Java program which brings two variables (String title and String imageUrl) then adds buttons named as the String (title).

When the user click on a button the application must load the image from the url in String (imageUrl).

The problem which I face is that none of the load images methodes worked with me!!

Although my project has no errors at all!!

This is my code:

public void start(String res) {
    try {
        JSONArray jarray = new JSONArray(jsonString);
        for (int k = 0; k < jarray.length(); k++) {
            JSONObject obj = jarray.getJSONObject(k);
            Button btn = new Button(this);
            btn.setText(obj.getString("title"));
            final String link = obj.getString("link"); 
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openImage(link);
                }
            });
            space.addView(btn);
            dialog.dismiss();
        }
    } catch(Exception e) {
    }
}
public void openImage(final String urk) {
    dialog = ProgressDialog.show(this, "", "loading...", true);
    new Thread(new Runnable() {
        public void run() {
            nextstepload(urk);
        }
    });
}
public void nextstepload(final String urk) {
    try {
        ImageView im = findViewById(R.id.img);
        URL url = new URL(urk);
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
        im.setImageBitmap(image); 
        dialog.dismiss(); 
    } catch(IOException e) {
        System.out.println(e);
    }
}

And my Xml form is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/displayer">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/img"/>
    </LinearLayout>
</LinearLayout>

Notice: I read the previous writings about this subject on StackOverFlow but none of them helps me enough! I may need a code suitable for my project especially.

Thanks very much

2
  • Use an image-loading library, such as Glide or Picasso, rather than rolling your own image-loading code. Beyond that, log the value of urk and make sure that it seems to look like what you expect. Commented Apr 18, 2020 at 20:27
  • Unfortunately I'm writing my code on AIDE IDE so I don't think there is a way to add maven libraries and use it. Are these libraries available as jar file? Commented Apr 18, 2020 at 20:35

2 Answers 2

2

Give it a try to Glide. https://github.com/bumptech/glide

You can load images like this:

Glide
    .with(context)
    .load(YOUR URL HERE - AS STRING)
    .placeholder(R.drawable.loading_spinner) // if you want some placeholder, it comes here
    .into(myImageView); // The imageview reference.
Sign up to request clarification or add additional context in comments.

Comments

0

You just forgot to Thread.start()

new Thread(new Runnable() {
    public void run() {
        nextstepload(urk);
    }
}).start();

A Thread object itself does nothing. You must .start() it after instantiated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.