android截击加载图片失败

android volley failed to load images

我希望应用程序的时间轴像 facebook,所以我遵循了这个 tutorial

时间轴工作正常,但 volley 无法加载个人资料图片和 post 图片。

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

}

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

使用 Picasso 在 ImageView 中加载图像。

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

试试这个

1.you 可以使用 Glide library 从 url 加载图像看下面的代码它可以以简单的方式帮助你

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

希望对你有用

我推荐你使用 Picasso 在 ImageView 中加载图片。 Picasso

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

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

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

并加载以使用此行

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