使用毕加索在recyclerview中的图像问题

时间:2016-09-30 04:49:11

标签: android android-recyclerview picasso

我正在从互联网上下载图片,这是我选择将其保存在手机上的路径:

ContextWrapper cw = new ContextWrapper(mContext);
File directory = cw.getDir("imagesDB", Context.MODE_PRIVATE);
OutputStream output = new FileOutputStream(new File(directory,"profile.jpg"));

下载图像后,我正在使用它来获取路径:

String databasePath = mContext.getDir("", Context.MODE_PRIVATE).getAbsolutePath();
databasePath = databasePath + "imagesDB/profile.jpg";

然后在我的recyclerview适配器中使用它:

Picasso.with(context).load(databasePath).placeholder(R.mipmap.ic_launcher).into(holder.Photo);

我总是显示ic_launcher图像而不是我下载的图像。

我是否使用了错误的图片路径?

这是我用来下载图片的代码:

 URL url = null;
    try {
        url = new URL("http://192.168.0.100/app/image/image1.jpg");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    InputStream input = null;
    try {
        input = url.openStream();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        ContextWrapper cw = new ContextWrapper(mContext);
        File directory = cw.getDir("imagesDB", Context.MODE_PRIVATE);
        OutputStream output = new FileOutputStream(new File(directory,"profile.jpg"));

        try {
            byte[] buffer = new byte[10000];
            int bytesRead = 0;
            while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                output.write(buffer, 0, bytesRead);
            }
            String databasePath = mContext.getDir("", Context.MODE_PRIVATE).getAbsolutePath();
            Log.i("","Path1: "+ databasePath.toString());
            databasePath = databasePath + "imagesDB/profile.jpg";
            Log.i("","Path2: "+ databasePath.toString());
        } finally {
            output.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:0)

好的问题是我无法将String传递给.load(),而是我必须创建一个这样的新文件:

Picasso.with(context).load(new File(databasePath)).into(holder.artistPhoto);

顺便说一句,我在这篇文章中找到了解决方案: Picasso Load image from filesystem

答案 1 :(得分:0)

使用Picasso的Recycler适配器。其余的代码,如果有人需要它,我会上传。

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
        private ArrayList<DogInfo> mArrayListInfo;
        private Context context;

            public RecyclerAdapter(ArrayList<DogInfo> mArrayListInfo,Context context) {
                this.mArrayListInfo = mArrayListInfo;
                this.context=context;
            }

            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.lay_display,null);

                return new MyViewHolder(view);
            }

            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                DogInfo dogInfo=mArrayListInfo.get(position);
                holder.mTextView.setText(dogInfo.getmInfo());

                Picasso.with(context).load(dogInfo.getmImage()).into(holder.mImageView);

            }



            @Override
            public int getItemCount() {
                return mArrayListInfo.size();
            }

            public class MyViewHolder extends RecyclerView.ViewHolder {
                ImageView mImageView;
                TextView mTextView;
                public MyViewHolder(View itemView) {
                    super(itemView);


                    mImageView= (ImageView) itemView.findViewById(R.id.imgShow);
                    mTextView= (TextView) itemView.findViewById(R.id.txtShow);

                }
            }
        }