简体   繁体   English

我可以在哪里存储我的应用程序的私有图像(内部存储之外)?

[英]Where can I store private images for my app (outside of internal storage)?

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 ?新的MediaStore中是否存在这样的地方? It's fine if they're deleted when the app is deleted.如果在删除应用程序时将它们删除,那很好。

This solution needs to support API 21 or higher.此解决方案需要支持 API 21 或更高版本。

I know there are a lot of questions like this, but they're 10+ years old and a lot has changed since then.我知道有很多这样的问题,但他们已经有 10 多年的历史了,从那时起发生了很多变化。

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 .我一直在通过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.我注意到一个崩溃Fatal Exception: android.database.sqlite.SQLiteDiskIOException disk I/O error (code 4874 SQLITE_IOERR_SHMSIZE) ,在谷歌搜索后,我发现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!傻我!

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. Android 没有为您提供在应用程序内部存储之外存储私人照片(用户或其他应用程序无法访问)的地方。 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.我知道你可以反驳我说可以使用getExternalStoragePublicDirectory()将它们存储在外部存储中,但问题是它是用户和其他应用程序可以访问的共享目录,并且在删除应用程序时仍然存在。

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.所以我建议你使用getFilesDir()因为它返回的目录对用户是隐藏的,并且在删除应用程序时被删除。 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.这不会完全解决问题,但我想这是一个开始。

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.为您的用例使用推荐的存储访问框架,您应该使用getFilesDir()它将返回您的应用程序的内部存储,这是您的应用程序私有的。

Where should I be storing these images?我应该在哪里存储这些图像?

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. 1.在线:使用一些云服务器来存储用户数据。 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. 2.离线:告诉用户您只有有限的存储空间,存储更多图像需要删除一些较旧的图像。

3. Both: Store in device. 3.两者:存储在设备中。 When internal storage is running low, tell user to buy some type of premium subscription to store unlimited cloud photos.当内部存储空间不足时,告诉用户购买某种类型的高级订阅以存储无限量的云照片。

I would recommend checking out AWS Amplify which allows you to integrate your application with AWS services.我建议查看AWS Amplify ,它允许您将应用程序与 AWS 服务集成。 For what you have described, you could use amplify to give your app the ability to authenticate users and set up cloud storage on AWS S3 which you can configure to only allow users to access and edit files that they have uploaded.对于您所描述的内容,您可以使用 amplify 使您的应用程序能够对用户进行身份验证并在 AWS S3 上设置云存储,您可以将其配置为仅允许用户访问和编辑他们上传的文件。 One nice thing about AWS Amplify and AWS S3 cloud storage is that there is a free tier which allows you to develop your application for little to no cost.关于 AWS Amplify 和 AWS S3 云存储的一个好处是有一个免费层,允许您以很少甚至免费的成本开发应用程序。 Depending on the amount of data that you will be uploading, it may be quite a while before you surpass the free tier limits.根据您要上传的数据量,您可能需要很长时间才能超过免费套餐限制。

A guide like this one may help you learn more.这样的指南可以帮助您了解更多信息。

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.由于存储空间有限似乎是根本原因,我建议有意使用 UI 来鼓励用户使用现有图像而不是新图像。 (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.以这种方式使用 UI 并不会直接为您获得更多空间,但它可以通过引导用户使用现有图像来帮助您更有效地利用现有空间。 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". *如果您仍然需要在应用程序之外的设备上拥有更多存储空间,您可以尝试使用带有“隐私”加密的公共空间。

I am not sure if I fully understand your question, but writing a quick tip.我不确定我是否完全理解你的问题,但写一个快速提示。 try cloud services to store your images like AWS s3, cloudinary.尝试使用云服务来存储您的图像,例如 AWS s3、cloudinary。 Cloudinary is much cheaper if you want to try like 25gb/month free.如果您想免费试用 25gb/月,Cloudinary 会便宜得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM