5

I have ListView, where I have ImageView. I need to get the image from url and show in ImageView, but that's not working, the image is not visible. In that ListView I have a TextView and CheckBox too, but you not need it because that works. I'm using Glide. So what's the problem?

I set in glide placeholders and it loads the placeholders. I've done debug and I saw that the glide gets the image URL. But the image doesn't load.

Ok here's the item code.

public class LanguageItem {

String imagePath;

LanguageItem(String imagePath,) {
    this.imagePath = imagePath;
}

public String getImagePath() {
    return imagePath;
}

There are textView and checkbox too, but I'm not showing it to you, because that works fine.

Here the adapter.

public class LanguageAdapter extends BaseAdapter {

private Context context;
private LayoutInflater lInflater;
private ArrayList<LanguageItem> objects;

LanguageAdapter(Context context, ArrayList<LanguageItem> itemObj) {
    this.context = context;
    objects = itemObj;
    lInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

//amount of elements
@Override
public int getCount() {
    return objects.size();
}

//element by position
@Override
public Object getItem(int position) {
    return objects.get(position);
}

//id by position
@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.language_items, parent, false);
    }

    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);

Glide.with(context).load(objects.get(position).getImagePath()).thumbnail(0.5f).crossFade().into(imageView);


    return view;
}

And here's the fragment. I'm doing my work in fragment.

public class FragmentLanguage extends BaseFragment {

private static final String IMAGE = "IMAGE";
private ApiClient apiClient;
private ArrayList<LanguageItem> objS;
private LanguageAdapter adapter;

private View mainView;
private ListView listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.languages, container, false);
    listView = (ListView) mainView.findViewById(R.id.language_list);

    apiClient = ApiClient.getInstance();

    //calling methods
    fillData();
    showResult();

    return mainView;
}

public void fillData() {

    objS = new ArrayList<>();

    getLanguageCall();

}

public void getLanguageCall() {
    Call<ResponseBody> getLanguage = apiClient.getLanguage(SharedPreferencesManager.getInstance().getAccessToken());
    getLanguage.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                try {
                    String data = response.body().string();
                    JSONArray array = new JSONArray(data);


                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object = array.getJSONObject(i);
                        String languageName = object.getString("name");
                        String path = object.getString("image_path");
                        String real_path = "https://supportop.eu-gb.mybluemix.net" + path.substring(1, path.length());


                        Toast.makeText(context, real_path, Toast.LENGTH_SHORT).show();
                        objS.add(new LanguageItem(languageName,real_path, false));
                    }
                    adapter = new LanguageAdapter(getActivity(), objS);
                    listView.setAdapter(adapter);

                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            } 
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
        }
    });
}
}

Ok here's the code. I done debug and the Image url it gets successfully, but glide not loads it. Thank you for reading.

Ok and here's the layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
android:orientation="horizontal">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="45dp"
    android:layout_height="30dp"
    android:layout_gravity="center"
    android:layout_marginStart="20dp" />

And here the listView part.

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/language_list"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/linearLayout"
    android:layout_above="@+id/bottomNavigationView">

</ListView>
3
  • If you try only Glide.with(context).load(objects.get(position).getImagePath()).into(imageView); does it work? Anyway are you sure your Image url is good? Have your tried this? Glide.with(context).load("https://a-valid-url/myimage.png").thumbnail(0.5f).crossFade().into(imageView); Commented Mar 22, 2018 at 16:38
  • can you try the this Glide.with(mContext).load(Uri.fromFile(new File(objects.get(position).getImagePath())).into(imageView); & are you using any cache strategy ? Commented Mar 22, 2018 at 18:07
  • No I'm not using cache strategy Commented Mar 22, 2018 at 18:26

5 Answers 5

9

Try to use http inside https. I think that's a server problem.

Sign up to request clarification or add additional context in comments.

Comments

4

You can use Glide or Picasso. From an activity the code will like:

Picasso

  Picasso.get().load(url).resize(50, 50).centerCrop().into(imageView)

Glide

  Glide.with (context)
     .load ( "http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
     .into (imageView);

1 Comment

Latest Picasso library has a different syntax! Watch out! Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
4

I'm a total Android beginner and my problem was that I was missing the INTERNET permission (as I could read in the Logcat tab after an hour long hassle).

So try adding this to your AndroidManifest.xml:

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET" />
 <application ...
</manifest>

1 Comment

Hi Papa Mufflon. Thanks for your answer. The problem was in server side. Images came in https and the image not loaded because of server. As I changed it to http, the problem fixed. So If you will have this problem too, try to change the http to https or reverse. Hope this will help you in future. Thanks.
2

Use Request Options with glide

RequestOptions options = new RequestOptions() 
        .centerCrop() 
        .placeholder(R.mipmap.ic_launcher_round)
        .error(R.mipmap.ic_launcher_round);
Glide.with(this).load(image_url).apply(options).into(imageView);

Comments

1

I just added <uses-permission android:name="android.permission.INTERNET" /> and it worked finaly.

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.