Picasso Load image from filesystem

AndroidPicasso

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

Try this:

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

It work perfect for me.

Solution 5 - Android

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

Solution 6 - Android

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/

Solution 7 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionedrianView Question on Stackoverflow
Solution 1 - AndroidPatrick FavreView Answer on Stackoverflow
Solution 2 - AndroidegfconnorView Answer on Stackoverflow
Solution 3 - AndroidedrianView Answer on Stackoverflow
Solution 4 - AndroidAwesome CodeView Answer on Stackoverflow
Solution 5 - AndroidTarun UmathView Answer on Stackoverflow
Solution 6 - AndroidJorgesysView Answer on Stackoverflow
Solution 7 - AndroidJuniorView Answer on Stackoverflow