将公共文件保存在内部存储器中

发布于 2024-11-03 07:14:48 字数 433 浏览 0 评论 0原文

我有一个保存从服务器下载的文件的应用程序。这些文件不是我的应用程序的私有文件,其他应用程序也应该可以访问。我想知道当 SD 卡不存在时保存这些文件的正确路径是什么。对于SD卡,有众所周知的API -

getExternalStorageDirectory()

对于内部存储器中的应用程序的私有数据,有 -

Context.getFilesDir()

在某些设备上,内部存储器由/emmc/表示。

如果有人能详细说明/emmc/,那将非常有帮助。我知道它代表嵌入式存储卡,并非所有设备中都存在。但它真的代表内存吗?还是第三个记忆?

我应该使用 openFileOutput()MODE_WORLD_READABLE 保存文件吗?

I have an application that saves files downloaded from a server. These files are not private to my application and should be accessible to other applications as well. I want to know what would be the correct path to save these files when the SD card is ABSENT. For the SD card, there is the well known API -

getExternalStorageDirectory()

For the application's private data in the internal memory there is -

Context.getFilesDir()

On some devices, the internal memory is represented by /emmc/.

It will be really helpful if someone could elaborate on /emmc/. I know it stands for embedded Memory card and is not present in all the devices. But is it really representative of the internal memory? Or is it the third memory?

Should I save the files using openFileOutput() with MODE_WORLD_READABLE?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不喜欢何必死缠烂打 2024-11-10 07:14:48

我意识到这个问题已经过时并且有一个公认的答案,但我认为这个问题仍然相关,并且有一种方法可以实现提问者想要的东西。

换句话说,我相信询问者想要保存文件,并使其可由其他应用程序访问(例如查看),而不必考虑外部存储(例如额外的权限以及外部存储不存在的可能性)。

事实证明,您可以使自己的文件可供其他应用程序读取。

File file = new File( ctx.getCacheDir(), "picture" );               
...
file.setReadable( true, false );
  // makes file readable to other apps

// Get another app to view my picture
Uri uri = Uri.fromFile(file);          
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setDataAndType( uri, "image/*" );
ctx.startActivity( intent );

setReadable 使上面的代码对我有用 - 没有该行,查看器应用程序无法读取文件。请注意,setReadable 对整个目录进行操作,因此最好的解决方案可能是创建一个名为“public”的子目录并将文件放入其中。

I realize this is old and has an accepted answer, but I think the question is still relevant and that there is a way to achieve what the asker wants.

To paraphrase, I believe the asker wants to save a file, and make it accessible (e.g. for viewing) by other apps, without having to think about external storage (e.g. an additional permission and the possibility that the external storage is not present).

It turns out that you can make your own files readable by other apps.

File file = new File( ctx.getCacheDir(), "picture" );               
...
file.setReadable( true, false );
  // makes file readable to other apps

// Get another app to view my picture
Uri uri = Uri.fromFile(file);          
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setDataAndType( uri, "image/*" );
ctx.startActivity( intent );

The setReadable made the above code work for me - without that line the viewer app couldn't read the file. Note that setReadable operates on the whole directory, so the best solution is probably to create a sub-directory name 'public' and put your files in there.

顾北清歌寒 2024-11-10 07:14:48

在某些设备上,内部存储器由 /emmc/ 表示。

/emmc/ 可能存在于某些设备上,并且它可能是内部的,但应用程序可能无法访问它,并且它肯定不是 Android SDK 的一部分,除非它恰好是 getExternalStorageDirectory() 返回。

但是它真的代表内存吗?

不。

或者是第三个记忆?

询问您的设备制造商。

我应该使用 openFileOutput() 和 MODE_WORLD_READABLE 保存文件吗?

这是不可能抽象地回答的。您说您的文件“也应该可供其他应用程序访问”,但您没有说明为什么您希望任何其他应用程序关心您的文件。其他应用程序不会扫描您的目录中的文件 - 充其量,它们将允许用户浏览外部存储。拥有 MODE_WORLD_READABLE 文件的唯一原因是您的应用程序是否会触发另一个应用程序对该文件执行某些操作(例如,ACTION_VIEW 意图)。

On some devices, the internal memory is represented by /emmc/.

/emmc/ may exist on some devices, and it may be internal, but it may not be accessible to applications, and it certainly is not part of the Android SDK unless that happens to be what getExternalStorageDirectory() returns.

But is it really representative of the internal memory?

No.

Or is it the third memory?

Ask your device manufacturer.

Should I save the files using openFileOutput() with MODE_WORLD_READABLE ?

That is impossible to answer in the abstract. You say that your files "should be accessible to other applications as well", but you have not indicated why you expect any other application to care one bit about your files. Other applications will not be scanning your directories for files -- at best, they will allow users to browse external storage. The only reason to have a file that is MODE_WORLD_READABLE is if your application will be triggering another application to do something with the file (e.g., ACTION_VIEW Intent).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文