0

I am downloading an image from internet and this is the path I chose to save it on the phone:

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

After the image has been downloaded I'm using this to get the path:

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

And then in my recyclerview adapter I use this:

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

I always get the ic_launcher image displayed instead of the one I downloaded.

Am i using a wrong path for the image?

This is the code I use to download the image:

 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 Answers2

0

Ok the problem was that I can't pass a String to .load(), instead i have to create a new file like this:

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

By the way I found the solution in this post: Picasso Load image from filesystem

Community
  • 1
  • 1
0

Recycler Adapter Using Picasso. Rest of the code if any one needs ask for it I will upload.

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

                }
            }
        }
Sumedh Ulhe
  • 667
  • 5
  • 10