6

I am trying to load images from camera path in gridview with the help of Picasso 2.5.2

storage/emulated/0/DCIM/Camera/IMG_20150822_133220.jpg here is the path.

Also I tried the given solution for the issue in Github repo of picasso. But it doesn't solve my problem.

I tried with Transformation of picasso, but images not getting load from camera path.

I tried this

File imageFile = new File(data.path);
Picasso.with(mContext)
  .load(imageFile)
  .placeholder(R.drawable.default_error)
  .error(R.drawable.default_error)
  .resize(mItemSize, mItemSize)
  .centerCrop()
  .into(image);
Sagar Panwala
  • 922
  • 1
  • 8
  • 23

3 Answers3

0

You must pass a url into the load() method.

File imageFile = new File(data.path);

 try{
        String url = imageFile.toURI().toURL().toString(); 
        Picasso.with(mContext)
               .load(url)
               .placeholder(R.drawable.default_error)
               .error(R.drawable.default_error)
               .resize(mItemSize, mItemSize)
               .centerCrop()
               .into(image);   
    }catch (MalformedURLException exeption){
        exeption.printStackTrace();
    }
axay
  • 1,657
  • 2
  • 18
  • 22
0

Don't use hardcoded paths to access sd card content -- they're not universal across devices.

Use .getExternalStoragePublicDirectory() and the proper two-parameter File constructor instead:

File f = new File(
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
        "Camera/IMG_20150822_133220.jpg");
Picasso.with(mContext).load(f)./* ..;

UPD Oh actually, apart from that, they have an open bug that looks related, someone there says they worked around the issue using absolute path, so try

Picasso.with(context).load(new File(data.path).getAbsolutePath())...
Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59
0

I had a similar problem now and this is how I solved it. I'm trying to take a screenshot of my phone's screen and then I saved it down as a jpg file in this filepath:

filepath: /data/data/com.example.simon.myapp/app_report/report.jpg

However, Picasso was not loading this at all - it was just giving me a blank screen.

So then when I took the screenshot, I decided to remove the .jpg from the filename so that the filepath resembles this:

filepath: /data/data/com.example.simon.myapp/app_report/report

I then used the following code to access and display my screenshot with no further problems.

    ContextWrapper cw = new ContextWrapper(context);
    File directory = cw.getDir("report", Context.MODE_PRIVATE);
    file = new File(directory, "/report");
    Log.e("filepath", file.getPath());
    Picasso.with(context).load(file).memoryPolicy(MemoryPolicy.NO_CACHE).into(screenshot);

I know that you will need to get the jpg file extension at some future stage, so if you need to get the "report.jpg", simply rename the file using this method:

android, How to rename a file?

private boolean rename(File from, File to) {
    return from.getParentFile().exists() && from.exists() && from.renameTo(to);
} 
Community
  • 1
  • 1
Simon
  • 19,658
  • 27
  • 149
  • 217