I'm writing an android app that makes use of many images(dozens). These images, like most of the other data in the app, are updated from a remote database. For the data I am going to have a local database and sync it with the remote database every time the remote db is updated.
我正在编写一个android应用程序,它可以使用很多图片(几十张)。与应用程序中的大多数其他数据一样,这些图像是从远程数据库更新的。对于数据,我将拥有一个本地数据库,并在每次更新远程数据库时将其与远程数据库同步。
The remote database will store the images as URLs, and my app will download the images from these urls to display them in the app. So I could just have my local database sync with the remote one and I'll have the URL of all the image files I'm using and I can re download them from the remote server every time the app is run, but this is obviously slow and wastes a lot of data.
远程数据库将存储图像的URL,以及我的应用程序将从这些URL下载的图片来显示他们的应用程序,所以我可以有我的本地数据库同步与远程,我要所有的图像文件的URL我使用,我可以重新下载他们从远程服务器每次运行应用程序,但这显然是缓慢而浪费大量的数据。
What I want to do is, everytime the database is updated and needs to be synced, the app will sync its local db and download the new images from their URLs as usual, but then it will save the image files somewhere on the device, so next time the app is run it can just grab the images from the device.
我想做的是,每次更新数据库,需要同步,同步应用程序将其本地数据库和下载新的图像的url和往常一样,但是它将保存图像文件在设备上的某个地方,所以下次运行程序可以获取设备的照片。
I can't seem to find an effective way of doing this, perhaps it's because it's a bad idea to do it this way in the first place? Sharepreferences probably won't have enough room, external storage isn't available all the time, and I hear it's a bad idea to just store a big chunk of binary data(such as an image) in the local database.
我似乎找不到一种有效的方法来做这件事,也许是因为这样做是一个坏主意吧?Sharepreferences可能没有足够的空间,外部存储也不是一直可用的,我听说在本地数据库中存储大量的二进制数据(如图像)是个坏主意。
What are my options here?
我的选择是什么?
1 个解决方案
#1
2
Start with Android Storage Options.
从Android存储选项开始。
Further:
进一步指出:
external storage isn't available all the time
外部存储不是一直可用的
That's true in theory, but in practice, you'll have external storage 99% of the time, especially if you're developing for newer devices. Note that "external storage" and SD Card aren't the same thing -- the terminology here is confusing. For this, use getExternalFilesDir()
.
这在理论上是正确的,但在实践中,99%的时候都有外部存储,特别是在为新设备开发时。注意,“外部存储”和SD卡不是一回事——这里的术语令人费解。为此,使用getExternalFilesDir()。
Regardless, if your image storage is a reasonable amount (<100MB is reasonable IMHO), then you can just use internal storage. As of Android 3.x, this won't cause a device to run out of space like early devices did. In practice, you'll be fine 99% of the time. Here you might want to use the cache dir, returned by getCacheDir()
.
无论如何,如果您的映像存储量是合理的(<100MB是合理的IMHO),那么您可以使用内部存储。作为Android 3。x,这不会像早期设备那样导致设备耗尽空间。在实践中,99%的时候你都会没事。在这里,您可能希望使用getCacheDir()返回的缓存目录。
In one of my apps I have a large number of files that are synced with a CDN. I use the sync process and database to retrieve download URLs and MD5 hashes of the files. If the MD5 hash has changed, then I download the file again in the background. I simply store the file using the hash as the file-name, so I can easily resolve duplicate files. Periodically, I also walk the cacheDir to see whether there are any files that are no longer referenced in the database.
在我的一个应用程序中,我有大量与CDN同步的文件。我使用同步过程和数据库检索下载url和MD5散列文件。如果MD5散列已经更改,那么我将在后台再次下载该文件。我只是使用散列作为文件名称来存储文件,因此可以轻松地解析重复的文件。定期地,我还会运行cacheDir,以查看是否有任何文件在数据库中不再被引用。
#1
2
Start with Android Storage Options.
从Android存储选项开始。
Further:
进一步指出:
external storage isn't available all the time
外部存储不是一直可用的
That's true in theory, but in practice, you'll have external storage 99% of the time, especially if you're developing for newer devices. Note that "external storage" and SD Card aren't the same thing -- the terminology here is confusing. For this, use getExternalFilesDir()
.
这在理论上是正确的,但在实践中,99%的时候都有外部存储,特别是在为新设备开发时。注意,“外部存储”和SD卡不是一回事——这里的术语令人费解。为此,使用getExternalFilesDir()。
Regardless, if your image storage is a reasonable amount (<100MB is reasonable IMHO), then you can just use internal storage. As of Android 3.x, this won't cause a device to run out of space like early devices did. In practice, you'll be fine 99% of the time. Here you might want to use the cache dir, returned by getCacheDir()
.
无论如何,如果您的映像存储量是合理的(<100MB是合理的IMHO),那么您可以使用内部存储。作为Android 3。x,这不会像早期设备那样导致设备耗尽空间。在实践中,99%的时候你都会没事。在这里,您可能希望使用getCacheDir()返回的缓存目录。
In one of my apps I have a large number of files that are synced with a CDN. I use the sync process and database to retrieve download URLs and MD5 hashes of the files. If the MD5 hash has changed, then I download the file again in the background. I simply store the file using the hash as the file-name, so I can easily resolve duplicate files. Periodically, I also walk the cacheDir to see whether there are any files that are no longer referenced in the database.
在我的一个应用程序中,我有大量与CDN同步的文件。我使用同步过程和数据库检索下载url和MD5散列文件。如果MD5散列已经更改,那么我将在后台再次下载该文件。我只是使用散列作为文件名称来存储文件,因此可以轻松地解析重复的文件。定期地,我还会运行cacheDir,以查看是否有任何文件在数据库中不再被引用。