毕加索从文件系统加载图像

edr*_*ian 73 android picasso

我可以使用Picasso库从文件系统加载图像吗?

startActivityForResult用来让用户从他的图库中选择一张照片,然后想要显示所选图像.

我已经有了工作代码来获取图像文件系统Uri,但无法使该Picasso.load()方法工作.

pat*_*ckf 143

当然可以.它实际上很直接:

File f = new File("path-to-file/file.png")
Run Code Online (Sandbox Code Playgroud)

要么

File f = new File(uri)

Picasso.with(getActivity()).load(f).into(imageView);
Run Code Online (Sandbox Code Playgroud)

Picasso.with(getActivity()).load(uri).into(imageView);
Run Code Online (Sandbox Code Playgroud)

作品

  • 我不知道它是否是Picasso从文件系统加载图像所需的"特定"URI格式(以String格式).但是我使用了从ActivityResult返回的那个,直到我传递一个File对象而不是直接传递String时它才起作用. (8认同)
  • 我试图这样做,但这不起作用,我有一个文件从另一个活动到我的应用程序缓存,但毕加索不加载它... (6认同)

egf*_*nor 27

是的你可以.

尝试:

Picasso.with(context).load(new File(YOUR_FILE_PATH)).into(imageView);
Run Code Online (Sandbox Code Playgroud)

编辑

你也可以打个电话.load(YOUR_URI).


edr*_*ian 22

查看源代码我还发现您可以从文件系统加载file:图像路径添加字符串前缀的图像.例如:

file:path/to/your/image
Run Code Online (Sandbox Code Playgroud)

此外,使用startActivityForResult时,您将获得以下内容:

Uri imageContent = data.getData();
Run Code Online (Sandbox Code Playgroud)

然后,您可以Picasso.with(getContext()).load(imageContent.toString).into(imageView);直接调用而无需创建Cursor和查询图像路径.

  • 谢谢你,直到我看到你的答案需要"file:"前缀,我的工作才开始. (2认同)

小智 9

试试这个:

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

它对我来说很完美.


Tar*_*ath 6

> 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);
Run Code Online (Sandbox Code Playgroud)