我可以在哪里存储我的应用程序的私人图像(在内部存储之外)?
有一个地方可以在内部存储外存储和管理自己的图像?我不希望其他应用程序能够查看或访问这些图像。我应该使用外部存储吗?在新的MediaStore
中是否存在这样的地方?如果将应用程序删除时,则可以删除它们。
该解决方案需要支持API 21或更高。
我知道有很多这样的问题,但是它们十多年了,此后发生了很多变化。
用例 /背景< / strong>
我有一个应用程序,其中所有数据都在本地存储在设备上(没有外部服务器)。
用户可以为日记条目选择自定义背景图像。用户可以选择为其创建的每个期刊条目使用其他图像。他们可以根据需要创建尽可能多的日记条目。他们可能会重新访问那些日记帐分录。因此,我需要在应用程序的一生中存储一个未知数的图像。我一直在通过context.filesdir
从图库中从图库中选择的图像保存副本。
我注意到崩溃致命异常:android.database.sqlite.sqlitediskioexception磁盘I/O错误(代码4874 sqlite_ioerr_shmsize)
) google搜索,我发现此错误可能表明下面的文件系统量是下层文件系统的卷,
我担心的是,由于我存储的用户映像,我的应用程序已经用尽了内部存储空间。
我应该在哪里存储这些图像?我最初之所以选择内部存储,是因为我希望用户的图像合理地私密(因为我不知道他们是否存储敏感图像)。我还想确保即使删除了源图像(从用户媒体中选择),即使映像将始终可用。但是,我没有考虑过内部存储的限制。愚蠢的我!
Is there a place where I can store and manage my own images outside of internal storage? I don't want other apps to be able to see or access these images. Should I use external storage? Does such a place exist in the new MediaStore
? It's fine if they're deleted when the app is deleted.
This solution needs to support API 21 or higher.
I know there are a lot of questions like this, but they're 10+ years old and a lot has changed since then.
Use case / background
I have an app where all data is stored locally on the device (no external servers).
Users can choose custom background images for journal entries. A user could choose to use a different image for each journal entry they create. They can create as many journal entries as they want. They may revisit those journal entries. So, I need to store an unknown amount of images for the lifetime of the app. I've been saving a copy of the images the user picks from the gallery in my local app storage via context.filesDir
.
I noticed a crash Fatal Exception: android.database.sqlite.SQLiteDiskIOException disk I/O error (code 4874 SQLITE_IOERR_SHMSIZE)
and after googling, I found This error may indicate that the underlying filesystem volume is out of space.
My concern is that my app is running out of internal storage space because of the user images I'm storing.
Where should I be storing these images? I originally chose internal storage because I wanted my users' images to be reasonably private (since I don't know if they're storing sensitive images or not). I also wanted to make sure the images would always be available even if the source image (chosen from user's media) is deleted. However, I hadn't considered the limits imposed on internal storage. Silly me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
达希尔的答案是正确的。使用建议的用例的推荐存储访问框架,您应该使用
getfilesdir()
,它将返回应用程序的内部存储,这是您应用程序的私有存储。问题是由于我们无法控制的资源限制。但是,如果您真的想存储所有图像,则可以采用一些方法:
1。在线:使用一些云服务器存储用户数据。这可能会花费很多。
2。离线:告诉用户您只有有限的存储空间,并且存储更多图像需要删除一些较旧的图像。
3。两者:存储在设备中。当内部存储运行较低时,请告诉用户购买某种类型的高级订阅以存储无限的云照片。
Darshil's answer is correct. Using the recommended Storage Access Framework for your use case, you should use
getFilesDir()
which will return your app's internal storage, which is private to your app.problem is due to limit of resource which is out of our control. However if you really want to store all the images, you can take some approaches:
1. Online: Use some cloud servers for storing user data. This might cost you a lot.
2. Offline: Tell user that you have only the limited amount of storage and storing more images will require to delete some older ones.
3. Both: Store in device. When internal storage is running low, tell user to buy some type of premium subscription to store unlimited cloud photos.
Android在App的内部存储空间之外没有为您提供存储私人照片(无法由用户或其他应用程序访问)的地方。我知道您可以通过使用
getExternalStoragePublicDirectory()
来将它们存储在外部存储中,这与我矛盾,但问题是它是用户和其他应用程序可访问的共享目录删除。因此,我建议您使用
getfilesdir()
作为返回的目录被用户隐藏,并在删除应用时删除。并在应用程序中实现一个图像压缩工具,该工具在用户上传时会自动压缩图像,然后将其保存到内部存储中。这不会完全解决问题,但我想这是一个开始。Android does not provide a place for you to store private photos (can't be accessed by user or other apps) outside of app's internal storage. I know you can contradict me by saying that one can store them in external storage by using
getExternalStoragePublicDirectory()
but the problem is that it is a shared directory accessible to users and other apps and remains when the app is deleted.So I suggest you to use
getFilesDir()
as the directory returned by it is hidden from users and is deleted when the app is deleted. And also implement a image compressing tool in your app which automatically compresses images when uploaded by the user and then save it to internal storage. This won't solve the problem completely but I guess it's a start.由于有限的存储空间似乎是根本原因,我建议您有意使用UI鼓励用户在新图像上使用现有图像。 (除了其他答案中提供的技术选项之外,这还可以起作用
)在这种情况下,使用现有图像是1个单击预览,获得新图像需要打开新的视图并搜索新图像。
以这种方式使用UI并不能直接为您提供更多空间,但它可以通过指导用户使用现有图像来帮助您更有效地使用所拥有的空间。它还为您提供一个警告(或限制)在没有更多可用空间时添加更多图像的地方。
*如果您仍然必须在应用程序外的设备上有更多存储空间,则可以尝试使用“隐私”加密的公共空间。
With limited storage space appearing to be the root cause, I recommend intentionally using the UI to encourage users to use existing images over new images. (This can work in addition to the technical options provided in other answers)
For example, present the user with a list of available background images and a link to add a new image. In this case, using an existing image is 1 click with a preview, where getting a new image requires opening a new view and searching for the new image.
Using the UI in this way doesn't directly get you more space, but it can help you to more efficiently use the space you have by guiding users to use existing images. It also gives you a place to warn about (or limit) adding more images when there is not more space available.
*If you still must have more storage on the device outside of the app, you could try using the public space with encryption for "privacy".