android volley无法加载图片

时间:2017-07-25 12:26:23

标签: android android-volley networkimageview

我希望应用的时间轴像facebook一样,所以我按照tutorial

时间线工作正常,但是凌空未能加载个人资料照片和帖子图片。

here is my project structure

public class FeedListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<FeedItem> feedItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
    this.activity = activity;
    this.feedItems = feedItems;
}

@Override
public int getCount() {
    return feedItems.size();
}

@Override
public Object getItem(int location) {
    return feedItems.get(location);
}

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

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

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.feed_item, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();

    TextView name = (TextView) convertView.findViewById(R.id.name);
    TextView timestamp = (TextView) convertView
            .findViewById(R.id.timestamp);
    TextView statusMsg = (TextView) convertView
            .findViewById(R.id.txtStatusMsg);
    TextView url = (TextView) convertView.findViewById(R.id.txtUrl);
    NetworkImageView profilePic = (NetworkImageView) convertView
            .findViewById(R.id.profilePic);
    FeedImageView feedImageView = (FeedImageView) convertView
            .findViewById(R.id.feedImage1);

    FeedItem item = feedItems.get(position);

    name.setText(item.getName());

    // Converting timestamp into x ago format
    CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
            Long.parseLong(item.getTimeStamp()),
            System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
    timestamp.setText(timeAgo);

    // Chcek for empty status message
    if (!TextUtils.isEmpty(item.getStatus())) {
        statusMsg.setText(item.getStatus());
        statusMsg.setVisibility(View.VISIBLE);
    } else {
        // status is empty, remove from view
        statusMsg.setVisibility(View.GONE);
    }

    // Checking for null feed url
    if (item.getUrl() != null) {
        url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
                + item.getUrl() + "</a> "));

        // Making url clickable
        url.setMovementMethod(LinkMovementMethod.getInstance());
        url.setVisibility(View.VISIBLE);
    } else {
        // url is null, remove from the view
        url.setVisibility(View.GONE);
    }

    // user profile pic
    profilePic.setImageUrl(item.getProfilePic(), imageLoader);

    // Feed image
    if (item.getImge() != null) {
        feedImageView.setImageUrl(item.getImge(), imageLoader);
        feedImageView.setVisibility(View.VISIBLE);
        feedImageView
                .setResponseObserver(new FeedImageView.ResponseObserver() {
                    @Override
                    public void onError() {
                    }

                    @Override
                    public void onSuccess() {
                    }
                });
    } else {
        feedImageView.setVisibility(View.GONE);
        Log.i("null","image");
    }

    return convertView;
}

}

如果您需要有关代码的更多信息,请通知我 提前谢谢。

4 个答案:

答案 0 :(得分:1)

试试这个

1.您可以使用Glide library从网址加载图片,查看下面的代码,它可以帮助您以简单的方式

在gradle文件中编译此依赖项

compile 'com.github.bumptech.glide:glide:4.0.0-RC0'

加载像这样的图像

Glide.with(HomeClass.this)
            .load(url)
            .into(imageview);

2.如果您不想使用第三方库

,请执行此操作
 new DownloadImage(imamgeview).execute(url);

创建异步任务

public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImage(ImageView bmImage) {
    this.bmImage = (ImageView ) bmImage;
}

protected Bitmap doInBackground(String... urls) {
    String urldisplay = urls[0];
    Bitmap mIcon11 = null;
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.d("Error", e.getStackTrace().toString());

    }
    return mIcon11;
}

protected void onPostExecute(Bitmap result) {
    bmImage.setImageBitmap(result);
}
}

3使用Picasso在ImageView中加载图像。 gradle文件中的依赖

compile 'com.squareup.picasso:picasso:2.5.2'

比在此图像视图中加载图片

Picasso.with(activity).load(img_url).noFade().into(imageview);

我希望它适用于您的情况

答案 1 :(得分:0)

使用Picasso在ImageView中加载图像。

ImageView imageview_obj;
Picasso.with(activity).load(img_url).noFade().into(imageview_obj);

答案 2 :(得分:0)

我建议您使用Picasso在ImageView中加载图像。 Picasso

这是一个非常简单而强大的图像下载和缓存库。 Simple tutorial for Picasso library

答案 3 :(得分:0)

您是否在依赖项中添加了gradle文件

compile 'com.github.bumptech.glide:glide:4.0.0-RC0'

加载以使用此行

 Glide.with(getBaseContext()).load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .thumbnail(0.1f)
                    .into(yourImageview);