android - 如何在android内部存储中为图像创建子目录

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

我不确定应用程序将如何创建子目录和存储图像。可以提供给 Picasso 以在 Imageview 中加载图像的图像路径是什么?

编辑1

我想,我可以使用 getApplicationContext().getFilesDir().getAbsolutePath() 来获取路径,但我仍然不确定如何为配置文件和联系人创建子目录?

最佳答案

在 Android 中,内部存储中有一个目录,您的应用可以在其中存储任何类型的文件;你可以使用 Context.getFilesDir()检索它。

您也可以创建子目录,使用 File.mkdir() .我不知道 Picasso 是否会处理这个问题,但要确保您可以事先创建目录。确定路径的代码如下所示:

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

现在您可以使用此方法获取放置配置文件数据(包括图片)的目录。假设每个配置文件都有一个名为 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);

希望这对您有所帮助。

关于android - 如何在android内部存储中为图像创建子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33655711/

相关文章:

java - getQuantityString(id,quantity,formatArgs)不适用于韩文字符串

android - 使用 Android 连接到主机地址

ios - UITableViewCellAccessoryDisclosureIndicator 的人字形图像名称是什么?

Java - getImage 和drawImage 不适用于本地文件

android - cloudBackend.setCredential 未设置 CloudEntity 的 createdBy/updatedBy/owner 属性

android - 广播接收器 onReceive() 从未调用过

python - 使用共享 C 库的 3D numpy 数组迭代

Android Picasso Image Viewer - 内存不足异常内存泄漏

java - 当使用 Picasso 显示占位符时,如何重新显示进度条?

java - 在两个图像之间循环