80

Can I use Picasso library to load images from the filesystem?

I'm using startActivityForResult to let the user pick a photo from his gallery, and then want to show the selected image.

I already have working code to get the image filesystem Uri, but can't get the Picasso.load() method to work.

PPartisan
  • 8,173
  • 4
  • 29
  • 48
edrian
  • 4,501
  • 5
  • 31
  • 35

7 Answers7

164

Of course you can. Its actually pretty straight forward:

File f = new File("path-to-file/file.png");

or

File f = new File(uri);

Picasso.get().load(f).into(imageView);

also

Picasso.get().load(uri).into(imageView);

works

Patrick
  • 33,984
  • 10
  • 106
  • 126
  • 10
    I don't know if it's a "specific" URI format that Picasso requires to load the images from the filesystem (in String format). But I used the one returned from ActivityResult and it didn't work until I passed a File object, instead of the String directly. – Gonan Jan 27 '16 at 18:26
  • 6
    I am trying to do that but this is not working, i got a File from another activity to my app cache but Picasso dont load it... – Loenix Jan 28 '16 at 17:09
  • 1
    patrickf's answer does the trick, however since Picasso has changed a little since the answer was posted, you'll need to use the following syntax: `Picasso.get().load(f).into(imageView);` or `Picasso.get().load(uri).into(imageView);` or `Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);` – Tom Larcher May 22 '19 at 06:45
30

Yes you can.

Try:

Picasso.with(context).load(new File(YOUR_FILE_PATH)).into(imageView);

EDIT

You can also call .load(YOUR_URI) instead as well.

NASSER
  • 5,900
  • 7
  • 38
  • 57
egfconnor
  • 2,637
  • 1
  • 26
  • 44
25

Looking in the source code I also discover that you can load the image from filesystem adding file: string prefix to your image path. For example:

file:path/to/your/image

Also, when using startActivityForResult, you will get something like this:

Uri imageContent = data.getData();

Then, you can call Picasso.with(getContext()).load(imageContent.toString).into(imageView); directly without need to create a Cursor and querying for the image path.

edrian
  • 4,501
  • 5
  • 31
  • 35
  • 3
    Thank you, mine didn't work until I saw your answer that the "file:" prefix is needed. – henrykodev Oct 24 '15 at 11:23
  • I don't know why it doesn't work. There is my path - "file:/storage/emulated/0/Android/data/com.fittingroom.newtimezone/files/default/AvatarPackage/DEFAULT_MY_AVATAR/pose1.jpeg" , but any result ( – Sirop4ik Feb 23 '17 at 13:24
  • 1
    @AlekseyTimoshchenko it should start with `file://`. Your Uri is missing the second `/`. – Sufian Mar 09 '17 at 13:53
11

Try this:

Picasso.with(context)
.load("file://"+path) // Add this
.config(Bitmap.Config.RGB_565)
.fit().centerCrop()
.into(imageView);

It work perfect for me.

Awesome Code
  • 141
  • 1
  • 3
  • mh, I would use RGB_8888 unless you have issues with memory constraints due to the image being really big – Zharf Mar 06 '17 at 14:42
7
> Picasso.get().load(R.drawable.landing_screen).into(imageView1);
> Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2);
> Picasso.get().load(new File(...)).into(imageView3);
Tarun Umath
  • 900
  • 10
  • 7
2

Basically we need three things, Context, image´s path and the ImageView Container

Old version of Picasso:

 Picasso.with(context).load("/files/my_image.jpg").into(myImageView);

Newer version of Picasso:

 Picasso.get().load("/files/my_image.jpg").into(myImageView);

but we can make use of more options:

  .resize(20, 20)
  .centerCrop()
  .placeholder(R.drawable.user_placeholder)
  .error(R.drawable.user_placeholder_error)

etc...

more info : http://square.github.io/picasso/

Nadeem Iqbal
  • 2,357
  • 1
  • 28
  • 43
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

If anyone trying to do this with Kotlin then here it is...

//variables

private lateinit var addImage: ImageView  // set the id of your ImageView
private lateinit var imageUri: Uri

//open gallery to select an image

val gallery = Intent()
        gallery.type = "image/*"
        gallery.action = Intent.ACTION_GET_CONTENT

        startActivityForResult(Intent.createChooser(gallery, "Select picture"), PICK_IMAGE)

//next

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
            imageUri = data?.data!!
            try {

                Picasso.get()
                    .load(imageUri)
                    .into(addImage)

            } catch (e: Throwable) {
                e.printStackTrace()
            }
        }
    }

That's all you need.

Junior
  • 170
  • 2
  • 10