Kum*_*eta 3 android image picasso
我正在编写一个应用程序来存储用户个人资料信息和联系信息。用户个人资料和联系信息也有图像/位图。我想可能将图像存储在内部存储器中并使用Picasso库显示图像。我想应用程序创建一个配置文件目录来存储配置文件图像和类似的联系人。我想用来Picasso从文件中检索图像并将其显示在 ImvageView 上,如下所示。
Picasso.with(context)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.load("file:///somepath/profile.png")
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
我不确定应用程序将如何创建子目录并存储图像。可以提供Picasso给加载图像的图像的路径是什么Imageview?
编辑1
我想,我可以getApplicationContext().getFilesDir().getAbsolutePath()用来获取路径,但我仍然不确定如何为个人资料和联系人创建子目录?
在 Android 中,内部存储中有一个目录,您的应用程序可以在其中存储任何类型的文件;您可以使用Context.getFilesDir()来检索它。
您也可以使用File.mkdir()创建子目录。我不知道毕加索是否会处理这个问题,但要确保您可以事先创建目录。确定路径的代码如下所示:
File makeAndGetProfileDirectory(String profileName) {
// determine the profile directory
File profileDirectory = new File(context.getFilesDir(), profileName);
// creates the directory if not present yet
profileDirectory.mkdir();
return profileDirectory;
}
Run Code Online (Sandbox Code Playgroud)
现在您可以使用此方法获取放置配置文件数据(包括图片)的目录。假设每个配置文件都有一个名为picture.jpg; Picasso 的代码如下所示:
String profileName = "foo"; // replace with the profile you want to show
File profileDir = makeAndGetProfileDirectory(profileName);
File profilePictureFile = new File(profileDir, "picture.jpg");
// now use Picasso to read it
Picasso.with(context)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.load(profilePictureFile)
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
2362 次 |
| 最近记录: |