Glide不会从网址加载图片

时间:2018-03-22 16:09:44

标签: java android retrofit2 android-glide

我有ListView,我有ImageView。我需要从网址获取图片并在ImageView中显示,但这不起作用,图片不可见。在那个ListView我也有TextViewCheckBox,但你不需要它,因为它有效。我正在使用Glide。所以有什么问题?

我设置滑动占位符并加载占位符。我已完成调试,我看到滑行获取图像URL。但图像无法加载。

好的,这是项目代码。

public class LanguageItem {

String imagePath;

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

public String getImagePath() {
    return imagePath;
}

还有textView和复选框,但我没有显示你,因为它可以。

这里是适配器。

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;

}

这是片段。我正在以片段形式完成我的工作。

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();
        }
    });
}

}

好的,这是代码。我完成了调试和Image url它成功,但滑动不加载它。谢谢你的阅读。

好的,这是布局。

<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" />

这里是listView部分。

<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>

5 个答案:

答案 0 :(得分:4)

尝试在https中使用http。我认为这是一个服务器问题。

答案 1 :(得分:2)

我是一个Android初学者,我的问题是我missing the INTERNET permission(一个小时的麻烦之后,我可以在Logcat标签中阅读)。

因此,请尝试将其添加到您的AndroidManifest.xml

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

答案 2 :(得分:1)

我刚刚添加了<uses-permission android:name="android.permission.INTERNET" />,它最终发挥了作用。

答案 3 :(得分:0)

使用带滑动的请求选项

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);

答案 4 :(得分:0)

您可以使用GlidePicasso。从代码中的活动开始:

<强>毕加索

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

<强>滑翔

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