Android 写入SD卡

Android 写入SD卡,android,android-manifest,Android,Android Manifest,我正在将一些文件写入SD卡。我的android版本是2.1。我没有在清单文件中设置任何权限,但它允许我写入sd卡 请让我知道为什么会有这种行为。仅将清单控件中的外部存储写入SD卡或我需要执行的任何其他配置 谢谢 Gururaja B O您是否使用API级别3或更低的版本编译?该权限是在API级别4(Android 1.6)中添加的,因此所有使用API级别3编译的应用程序(为了与仍在运行1.5的手机兼容)都将自动拥有该权限。我创建了一个utils方法来检查设备上是否有SD卡,并获取设备上的SD卡路

我正在将一些文件写入SD卡。我的android版本是2.1。我没有在清单文件中设置任何权限,但它允许我写入sd卡

请让我知道为什么会有这种行为。仅将清单控件中的外部存储写入SD卡或我需要执行的任何其他配置

谢谢
Gururaja B O

您是否使用API级别3或更低的版本编译?该权限是在API级别4(Android 1.6)中添加的,因此所有使用API级别3编译的应用程序(为了与仍在运行1.5的手机兼容)都将自动拥有该权限。

我创建了一个utils方法来检查设备上是否有SD卡,并获取设备上的SD卡路径(如果有)

您可以将下面的2个方法复制到您需要的项目类中。在返回的路径上创建项目文件夹并在其上写入数据。就这些

public String isRemovableSDCardAvailable() {
    final String FLAG = "mnt";
    final String SECONDARY_STORAGE = System.getenv("SECONDARY_STORAGE");
    final String EXTERNAL_STORAGE_DOCOMO = System.getenv("EXTERNAL_STORAGE_DOCOMO");
    final String EXTERNAL_SDCARD_STORAGE = System.getenv("EXTERNAL_SDCARD_STORAGE");
    final String EXTERNAL_SD_STORAGE = System.getenv("EXTERNAL_SD_STORAGE");
    final String EXTERNAL_STORAGE = System.getenv("EXTERNAL_STORAGE");

    Map<Integer, String> listEnvironmentVariableStoreSDCardRootDirectory = new HashMap<Integer, String>();
    listEnvironmentVariableStoreSDCardRootDirectory.put(0, SECONDARY_STORAGE);
    listEnvironmentVariableStoreSDCardRootDirectory.put(1, EXTERNAL_STORAGE_DOCOMO);
    listEnvironmentVariableStoreSDCardRootDirectory.put(2, EXTERNAL_SDCARD_STORAGE);
    listEnvironmentVariableStoreSDCardRootDirectory.put(3, EXTERNAL_SD_STORAGE);
    listEnvironmentVariableStoreSDCardRootDirectory.put(4, EXTERNAL_STORAGE);

    File externalStorageList[] = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        externalStorageList = getContext().getExternalFilesDirs(null);
    }
    String directory = null;
    int size = listEnvironmentVariableStoreSDCardRootDirectory.size();
    for (int i = 0; i < size; i++) {
        if (externalStorageList != null && externalStorageList.length > 1 && externalStorageList[1] != null)
            directory = externalStorageList[1].getAbsolutePath();
        else
            directory = listEnvironmentVariableStoreSDCardRootDirectory.get(i);

        directory = canCreateFile(directory);
        if (directory != null && directory.length() != 0) {
            if (i == size - 1) {
                if (directory.contains(FLAG)) {
                    Log.e(getClass().getSimpleName(), "SD Card's directory: " + directory);
                    return directory;
                } else {
                    return null;
                }
            }
            Log.e(getClass().getSimpleName(), "SD Card's directory: " + directory);
            return directory;
        }
    }
    return null;
}

/**
 * Check if can create file on given directory. Use this enclose with method
 * {@link BeginScreenFragement#isRemovableSDCardAvailable()} to check sd
 * card is available on device or not.
 * 
 * @param directory
 * @return
 */
public String canCreateFile(String directory) {
    final String FILE_DIR = directory + File.separator + "hoang.txt";
    File tempFlie = null;
    try {
        tempFlie = new File(FILE_DIR);
        FileOutputStream fos = new FileOutputStream(tempFlie);
        fos.write(new byte[1024]);
        fos.flush();
        fos.close();
        Log.e(getClass().getSimpleName(), "Can write file on this directory: " + FILE_DIR);
    } catch (Exception e) {
        Log.e(getClass().getSimpleName(), "Write file error: " + e.getMessage());
        return null;
    } finally {
        if (tempFlie != null && tempFlie.exists() && tempFlie.isFile()) {
            // tempFlie.delete();
            tempFlie = null;
        }
    }
    return directory;
}
public字符串isRemovableSDCardAvailable(){
最终字符串标志=“mnt”;
最终字符串SECONDARY_STORAGE=System.getenv(“SECONDARY_STORAGE”);
最终字符串EXTERNAL_STORAGE_DOCOMO=System.getenv(“EXTERNAL_STORAGE_DOCOMO”);
最终字符串EXTERNAL_SDCARD_STORAGE=System.getenv(“EXTERNAL_SDCARD_STORAGE”);
最终字符串EXTERNAL_SD_STORAGE=System.getenv(“EXTERNAL_SD_STORAGE”);
最终字符串EXTERNAL_STORAGE=System.getenv(“EXTERNAL_STORAGE”);
Map ListenEnvironment VariableStoresCardRootDirectory=new HashMap();
ListenEnvironmentVariableStoresCardRootDirectory.put(0,辅助存储);
ListenEnvironmentVariableStoresCardRootDirectory.put(1,外部存储\uDoCoMo);
ListenEnvironmentVariableStoresDcardRootDirectory.put(2,外部存储卡);
ListenEnvironmentVariableStoresCardRootDirectory.put(3,外部存储);
ListenEnvironmentVariableStoresCardRootDirectory.put(4,外部存储);
文件externalStorageList[]=null;
if(android.os.Build.VERSION.SDK\u INT>=android.os.Build.VERSION\u code.KITKAT){
externalStorageList=getContext().getExternalFilesDirs(null);
}
字符串目录=null;
int size=listenvironmentvariablestoresCardRootDirectory.size();
对于(int i=0;i1&&externalStorageList[1]!=null)
directory=externalStorageList[1]。getAbsolutePath();
其他的
directory=listenvironmentvariablestoresCardRootDirectory.get(i);
directory=canCreateFile(目录);
if(directory!=null&&directory.length()!=0){
如果(i==大小-1){
if(directory.contains(标志)){
Log.e(getClass().getSimpleName(),“SD卡目录:“+目录”);
返回目录;
}否则{
返回null;
}
}
Log.e(getClass().getSimpleName(),“SD卡目录:“+目录”);
返回目录;
}
}
返回null;
}
/**
*检查是否可以在给定目录上创建文件。使用此方法
*{@link BeginScreenFragement#isRemovableSDCardAvailable()}检查sd
*卡是否在设备上可用。
* 
*@param目录
*@返回
*/
公共字符串canCreateFile(字符串目录){
最后一个字符串FILE_DIR=directory+FILE.separator+“hoang.txt”;
文件tempFlie=null;
试一试{
tempFlie=新文件(文件目录);
FileOutputStream fos=新的FileOutputStream(tempFlie);
fos.write(新字节[1024]);
fos.flush();
fos.close();
Log.e(getClass().getSimpleName(),“可以在此目录上写入文件:”+file\u DIR);
}捕获(例外e){
Log.e(getClass().getSimpleName(),“写入文件错误:”+e.getMessage());
返回null;
}最后{
if(tempFlie!=null&&tempFlie.exists()&&tempFlie.isFile()){
//tempFlie.delete();
tempFlie=null;
}
}
返回目录;
}

我正在使用Eclair代码库构建系统映像,其API级别大于3。除了检查API级别之外,我是否需要修改任何其他配置?