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
urkand make sure that it seems to look like what you expect.