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();
        }
    }
10
  • First print databasePath in Log and check its value.
    – Piyush
    Sep 30, 2016 at 4:51
  • I think image takes time from server to download and save. and you are attaching image using picaso before download it.
    – D.J
    Sep 30, 2016 at 4:52
  • Have you given read write permission in your manifest? Also check that you get proper path in your variable
    – GrIsHu
    Sep 30, 2016 at 4:52
  • Try using AsyncTask, download image in doInBackground() and use picasso in onPostExecute() Sep 30, 2016 at 5:09
  • please provide the image url for same Sep 30, 2016 at 5:32

2 Answers 2

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

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

                }
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.