如何让用户访问应用程序的内部存储目录? [英] How can I let users access the internal storage directory of my app?

查看:226
本文介绍了如何让用户访问应用程序的内部存储目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序将文件存储在其内部存储目录中(/Android/data/com.mycompany.myapp/files,由getFilesDir()返回),我希望允许用户直接从文件管理中访问这些文件移动设备上的应用程序或Android File Transfer桌面应用程序.

My app stores files in its internal storage directory (/Android/data/com.mycompany.myapp/files, as returned by getFilesDir()), and I would like to allow users to access those files directly from a file management app on their mobile device or the Android File Transfer desktop appplication.

存储选项开发者指南说:

默认情况下,保存到内部存储器的文件是您的应用程序专用的,其他应用程序无法访问它们(用户也不能访问).

By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user).

默认"表示我可以更改默认权限以允许用户访问这些文件,但是找不到有关如何执行此操作的任何文档.当前,当我使用文件管理应用程序浏览/Android/data目录时,com.mycompany.myapp目录处于隐藏状态.

"By default" implies that I can change the default permissions to allow users to access these files, but I can't find any documentation of how to do that. Currently the com.mycompany.myapp directory is hidden when I browse the /Android/data directory with a file management app.

我目前正在保存文件数据,如下所示:

I'm currently saving file data like this:

String fileName = "myfile.jpg";
String filePath = getFilesDir() + File.separator + fileName;
FileOutputStream fileStream = new FileOutputStream(filePath);
fileData.writeTo(fileStream); // fileData is a ByteArrayOutputStream
fileStream.close();

我尝试将单个文件的权限设置为全球可读,但这并没有使目录可见:

I tried setting the permissions of the individual files to world-readable, but this didn't make the directory visible:

FileOutputStream fileStream = this.app.openFileOutput(fileName, Context.MODE_WORLD_READABLE);

我还检查了AndroidManifest文件的文档,但没有看到任何内容.有人知道我该怎么做吗?

I also checked the documentation for the AndroidManifest file and didn't see anything there. Does anyone know how I can do this?

推荐答案

我仔细查看了getFilesDir() vs getExternalFilesDir()的结果,发现getFilesDir()返回/data/data/[packagename]/files,而getExternalFilesDir()返回.我以为我在/Android/data中浏览的应用程序文件是内部存储目录,但是现在我看到这些实际上是外部存储目录.

I took a closer look at the result of getFilesDir() vs getExternalFilesDir() and found that getFilesDir() returns /data/data/[packagename]/files while getExternalFilesDir() returns /Android/data/[packagename]/files. I thought the app files I was browsing in /Android/data were the internal storage directories, but now I see that those are actually the external storage directories.

如果内部存储目录的确对普通用户绝对不可用,我希望文档说,而不是说它们默认情况下不可用".至少对我来说,说默认"意味着可以更改默认行为.

If the internal storage directories are indeed never available to regular users, I wish the documentation said that instead of saying they are not available "by default." To me at least, saying "by default" implies that the default behavior can be changed.

无论如何,我测试并确认,如果删除我的应用程序,保存到getExternalFilesDir()的文件将被自动删除.这样可以满足我对与该应用程序明确连接的存储位置的需求(使用特定于应用程序的目录名称,并随该应用程序一起删除),但用户偶尔可以对其进行手动文件管理.

Anyway, I tested and confirmed that if I delete my app, files saved to the getExternalFilesDir() are deleted automatically. So that meets my need for a storage location that is clearly connected with the app (uses an app-specific directory name and is deleted with the app) but is accessible to users for occasional manual file management.

下面的比较可能会对其他阅读此书的人有所帮助:

Here's a comparison that might be helpful to someone else reading this:

  • getFilesDir()-创建特定于应用程序的目录;对用户隐藏;已通过应用删除
  • getExternalFilesDir()-创建特定于应用程序的目录;用户可访问的;已通过应用删除
  • getExternalStoragePublicDirectory()-使用共享目录(例如,音乐);用户可访问的;删除应用程序后仍然存在
  • getFilesDir() - creates an app-specific directory; hidden from users; deleted with the app
  • getExternalFilesDir() - creates an app-specific directory; accessible to users; deleted with the app
  • getExternalStoragePublicDirectory() - uses a shared directory (e.g., Music); accessible to users; remains when the app is deleted

这篇关于如何让用户访问应用程序的内部存储目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆