简体   繁体   English

毕加索从手机加载图片时出错

[英]Picasso error loading image from the phone

I have a GridView in which I put some Images picked from the phone using Picasso (I'm targeting only KitKat at the moment). 我有一个GridView,其中放置了一些使用毕加索从手机中拾取的图像(目前我仅瞄准KitKat)。 I'm passing an ArrayList of Uri to the Adapter and all works fine. 我将Uri的ArrayList传递给适配器,并且一切正常。 But I want that the images stay here also after the activity is destroyed so I'm using inside my Fragment: 但是我希望图像在活动被破坏后也留在这里,所以我在Fragment中使用:

@Override
public void onPause() {
    imagesUriSet = new HashSet<String>();
    for(Uri i : imagesUri)
        imagesUriSet.add(i.toString());
    mSharedPreferences.edit().putStringSet("IMAGES",imagesUriSet).commit();
    super.onPause();
}

Where imagesUri is the ArrayList with the Uri of the images I want to be in the GridViwe. 其中imagesUri是具有要在GridViwe中出现的图像的Uri的ArrayList。 Using onPause may not be the best choice but it's not important for me at the moment. 使用onPause可能不是最佳选择,但目前对我而言并不重要。

On the onCreateView of my Fragment 在片段的onCreateView上

    Set<String> set = mSharedPreferences.getStringSet("IMAGES", null);

    if(set != null)
        for(String uriString : set) {
            imagesUri.add(Uri.parse(uriString));
            Log.i("Immagine: ", Uri.parse(uriString) + "");
        }

    gridViewAdapter.notifyDataSetChanged();

The Adapter: 适配器:

public class GridViewAdapter extends ArrayAdapter {
    private final Context context;
    private ArrayList data = new ArrayList();

    public GridViewAdapter(Context context, int layoutResourceId,
                           ArrayList data) {
        super(context, layoutResourceId, data);
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SquaredImageView view = (SquaredImageView) convertView;
        if (view == null) {
            view = new SquaredImageView(context);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }

        Uri url = (Uri) data.get(position);
        Picasso.with(context) //
                .load(url) //
                .fit() //
                .error(R.drawable.error)
                .into(view);

        return view;
    }
}

If I launch my app, the images that I saved in my SharedPreferences aren't displayed, and I see the error image instead. 如果启动我的应用程序,则不会显示我保存在SharedPreferences中的图像,而是看到错误图像。 But if I add the same image again, then I can see also the first one. 但是,如果我再次添加相同的图像,那么我也可以看到第一个图像。 I'm not sure what's happening. 我不确定发生了什么。 What's the problem? 有什么问题?

Did you try and do a Log.I and see what is being both written to and read from SharedPreferences to see if it actually writes/reads the correct values? 您是否尝试过执行Log.I,并查看正在写入和读取SharedPreferences的内容,以查看它是否实际写入/读取正确的值? And also try to do Log.I and see if the correct URL is being read when you're doing "Uri url = (Uri) data.get(position);" 并尝试执行Log.I,并在执行“ Uri url =(Uri)data.get(position);”时查看是否正在读取正确的URL。 in the getView() method. 在getView()方法中。

My best guess is somewhere along the way, it fails to set/get the correct data from SharedPreferences. 我最好的猜测是在此过程中的某个地方,它无法从SharedPreferences设置/获取正确的数据。

I'm currently also using Picasso to load a lot of pictures (Facebook/Twitter/LinkedIn profile pictures) in a ListView, though i'm not saving the URL addresses in SharedPreferences, i'm saving them in a database (along with other information). 我目前还使用Picasso在ListView中加载很多图片(Facebook / Twitter / LinkedIn个人资料图片),尽管我没有将URL地址保存在SharedPreferences中,而是将它们保存在数据库中(以及其他信息)。 I haven't had any issues with it at all. 我根本没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM