问题
I am working on an application that needs the ability to create files on the removable sd card in the Samsung S4 i9295 device. I am running a stock Samsung ROM 5.0.1 that has been rooted. For test purposes, I have been trying to create these files by hardcoding the paths for this specific device, but have been unable to create files on the removable card. The code below creates files as expected on the internal sd card but not on the removable card.
//creates directory on internal sdcard
public void createInternalSdcardFile(){
Log.v(LOGTAG, "Create internal sdcard file initiated");
String szFilePath = "/storage/sdcard0";
File appDir = new File(szFilePath, "TargetFile");
appDir.mkdir();
makeTimestamp();
File exportDir = new File(szFilePath, "TargetFile/" + szDateTime);
exportDir.mkdir();
}
//creates a directory at the root of the REMOVEABLE sdcard
public void createRemoveableSdcardFile(){
Log.v(LOGTAG, "Create removeable sdcard file initiated");
String szFilePath = "/storage/extSdCard";
File appDir = new File(szFilePath, "TargetFile");
appDir.mkdir();
makeTimestamp();
File exportDir = new File(szFilePath, "TargetFile/" + szDateTime);
exportDir.mkdir();
}
public void makeTimestamp(){
Date T = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
szDateTime = sdf.format(T);
}
A review of Logcat shows nothing suspicious. Relevant manifest permissions are:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_SECONDARY_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SECONDARY_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.STORAGE" />
File permissions for "/storage/extSdCard" on the device are owner has read, write, execute. Group has read, write, and execute as well. Others are only execute. File permissions for "/storage/sdcard0" are read, write, execute for everybody.
Why am I not able to write on the removeable card for this device? Specific suggestions for a solution would be appreciated.
Note: I am also working with getExternalFilesDirs()
and am able to write to the internal sdcard but not the removeable card as is also the case with static references. Static paths are used in the example code here for simplification.
回答1:
Got it. In general, replacing...
String szFilePath = "/mnt/extSdCard/temp";
with this:
File[] fo = getExternalFilesDirs(null);
File f =fo[1];
has resolved the issue. Specific changes to final test case are:
public void createRemoveableSdcardFile(View view) {
File[] fo = getExternalFilesDirs(null);
File f =fo[1];
File appDir = new File(f, "TargetFile");
appDir.mkdir();
makeTimestamp();
File exportDir = new File(f, "TargetFile/" + szDateTime);
exportDir.mkdir();
}
This creates a file on the removeable sdcard here:
../extsdcard/android/data/net.google.example/file/
来源:https://stackoverflow.com/questions/36780740/unable-to-create-file-on-removeable-sdcard-in-android-5-0-1