Android Picasso 从文件系统加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23681177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Picasso Load image from filesystem
提问by edrian
Can I use Picasso library to load images from the filesystem?
我可以使用 Picasso 库从文件系统加载图像吗?
I'm using startActivityForResult
to let the user pick a photo from his gallery, and then want to show the selected image.
我使用startActivityForResult
让用户从他的图库中选择一张照片,然后想要显示所选图像。
I already have working code to get the image filesystem Uri
, but can't get the Picasso.load()
method to work.
我已经有了获取图像文件系统的工作代码Uri
,但无法使该Picasso.load()
方法起作用。
回答by Patrick Favre
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
作品
回答by egfconnor
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.
您也可以.load(YOUR_URI)
改为调用。
回答by edrian
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:
,在图像路径中添加字符串前缀。例如:
file:path/to/your/image
Also, when using startActivityForResult, you will get something like this:
此外,当使用 startActivityForResult 时,您将得到如下信息:
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.
然后,您可以Picasso.with(getContext()).load(imageContent.toString).into(imageView);
直接调用而无需创建Cursor
和查询图像路径。
回答by Awesome Code
Try this:
尝试这个:
Picasso.with(context)
.load("file://"+path) // Add this
.config(Bitmap.Config.RGB_565)
.fit().centerCrop()
.into(imageView);
It work perfect for me.
它非常适合我。
回答by Tarun Umath
> 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);
回答by Jorgesys
Basically we need three things, Context
, image′s path
and the ImageView
Container
基本上我们需要三样东西Context
,image′s path
和ImageView
容器
//Old version: Picasso.with(context).load("/files/my_image.jpg").into(myImageView);
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/
回答by Junior
If anyone trying to do this with Kotlin then here it is...
如果有人试图用 Kotlin 做到这一点,那么这里是......
//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.
这就是你所需要的。