我想在外部SD卡中创建目录,我在MI(Android M),三星和联想(Android L)中测试了我的代码。下面的代码在除棒棒糖之外的所有设备中都能正常工作。
private void crateDirectory() {
Log.d(TAG, "inside crateDirectory() ");
try {
String extPath = "";
String base = "/storage/sdcard1/"; //lenovo
// String base="/storage/9F44-E930/"; // for xiaomi
File baseFile = new File(base);
Log.d(TAG, "baseFile.exists() >>" + baseFile.exists()); // resulting TRUE
String path = "Android/data/package name/files/";
extPath = base + path;
Log.d(TAG, "exPath >>" + extPath);
File myFile = new File(Environment.DIRECTORY_DOWNLOADS + "/MY Download");
Log.d(TAG, "myFile >>" + myFile.getAbsolutePath());
File outputFile = new File(extPath, "" + myFile.getAbsolutePath());
if (!outputFile.exists()) {
Log.d(TAG, "Creating outputFile ....");
outputFile.mkdirs();
}
Log.d(TAG, "outputFile.exists() >>" + outputFile.exists() + "\n outputFile >>" + outputFile.getAbsolutePath());
} catch (Exception e) {
Log.d(TAG, "Exception >>" + Log.getStackTraceString(e));
}
Log.d(TAG, "end of crateDirectory() ");
}