在 SD 卡上存储 Android 应用程序数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1209469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
storing android application data on SD Card
提问by user121196
Is there a way to store android application data on the SD card instead of in the internal memory? I know how to transfer the application sqlite database from the internal memory to the SDCard, but what if the internal memory gets full in the first place? How does everyone handle this?
有没有办法将 android 应用程序数据存储在 SD 卡上而不是内存中?我知道如何将应用程序sqlite数据库从内存传输到SDCard,但是如果内存首先被填满怎么办?这个大家都是怎么处理的?
采纳答案by haseman
Warning:This answer is out-dated. You should use Environment.getExternalStorageDirectory()to get the root path of the SD card as mentioned in the answers below.
警告:此答案已过时。您应该使用Environment.getExternalStorageDirectory()来获取 SD 卡的根路径,如以下答案中所述。
Old Answer so the comments on this make sense:
旧答案所以对此的评论是有道理的:
Adding /sdcard/
to the root your path should direct your Android application to use the SD card (at least it works that way with the G1). Android's file system objects give you the ability to check file sizes... so it should be possible (if tricky) to write some fail-over code. This code would adjust your root path if the internal memory filled up.
将/sdcard/
您的路径添加到根目录应该会引导您的 Android 应用程序使用 SD 卡(至少它可以与 G1 一起使用)。Android 的文件系统对象使您能够检查文件大小......因此应该可以(如果棘手)编写一些故障转移代码。如果内部存储器已满,此代码将调整您的根路径。
回答by yincrash
It's better practice to use Environment.getExternalStorageDirectory()than to hard code "/sdcard" It's not always certain that the folder name will be called that. Also, the Environment class offers a getExternalStorageState() method to check on if the external storage is even available.
使用Environment.getExternalStorageDirectory()比硬编码“/sdcard”更好并不总是确定文件夹名称将被称为。此外,Environment 类提供了一个 getExternalStorageState() 方法来检查外部存储是否可用。
回答by Rabih harb
To begin:
开始:
Depending on the model/os, you can access the sd card root directory with:
根据型号/操作系统,您可以使用以下命令访问 sd 卡根目录:
File externalStorage = Environment.getExternalStorageDirectory();
This will refer to the internal sd storage or internal sd memory.
这将指内部 sd 存储或内部 sd 内存。
externalStorage.getAbsolutePath()
will return one of the following values
将返回以下值之一
"/sdcard/" or "/mnt/sdcard/"
"/sdcard/" 或 "/mnt/sdcard/"
To access the external sd memory or micro SD, that you usually plug from the outside of the phone/tablet, you must use one of the following folders that android creates to point to the external memory:
要访问通常从手机/平板电脑外部插入的外部 SD 存储器或微型 SD,您必须使用 android 创建的以下文件夹之一指向外部存储器:
"/mnt/sdcard/sd"
"/mnt/sdcard/external_sd"
"/sdcard/external_sd"
"/sdcard/sd"
"/mnt/sdcard/"
ps: you can notice an empty folder external_sd
or sd
on the internal sdcard
ps:您可以注意到一个空文件夹external_sd
或sd
内部sdcard
memory, this folder is empty and its used to point to external micro sd card.
内存,此文件夹为空,用于指向外部 micro sd 卡。
at the end make sure that you have read/write access to the sd card android.permission.WRITE_EXTERNAL_STORAGE
in the android manifest xml.
最后确保您android.permission.WRITE_EXTERNAL_STORAGE
对 android manifest xml 中的 SD 卡具有读/写访问权限。
finally you must specify the file name and your ready
最后,您必须指定文件名并准备好
private SQLiteDatabase DB = null;
private static final String DATABASE_NAME = "MyDb.db";
////////////
File sdcard = Environment.getExternalStorageDirectory();
String dbfile = sdcard.getAbsolutePath() + File.separator+ "external_sd" + File.separator + DATABASE_NAME;
DB = SQLiteDatabase.openDatabase(dbfile, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);
///////////
and your ready to go ...
你准备好了......
回答by phreed
Here is another neat little trick. The Applicationhas a number of methods which are called to acquire paths. In particular the application has the method getDatabasePathwith is used by SQLiteOpenHelperto construct the path. A custom application class can override these methods to provide different paths including paths in the getExternalStorageDirectory.
这是另一个巧妙的小技巧。的应用有许多这是所谓的获取路径的方法。特别是该应用程序具有SQLiteOpenHelper使用getDatabasePath方法来构造路径。自定义应用程序类可以覆盖这些方法以提供不同的路径,包括getExternalStorageDirectory 中的路径。
The external storage is either application specific or public. There are methods, replacing the getExternalStorageDirectorymechanism, getExternalFilesDir()and getExternalStoragePublicDirectory()respectively.
外部存储要么是特定于应用程序的,要么是公共的。有一些方法,分别替换了getExternalStorageDirectory机制, getExternalFilesDir()和getExternalStoragePublicDirectory()。
回答by Loris Albanese
some device use /mnt/sdcard as root point to SD Card.
某些设备使用 /mnt/sdcard 作为根指向 SD 卡。
回答by Prashast
The problem with using the SDCard is that you cannot reliably assume that it will be present always when your application needs it. This is not the case with internal memory. As long as your application does not rely on this data to run it should be fine.
使用 SDCard 的问题在于您无法可靠地假设它会在您的应用程序需要时始终存在。内部存储器不是这种情况。只要您的应用程序不依赖于这些数据来运行它应该没问题。