Please read my question introduction before reading the real question:
In Android there are an internal storage
and an external storage
.
Don't be confused, because the internal storage
relates to files that are accessible only by the app, and the external storage
relates to normal files that can be accessed by all apps and can be found both in the real internal storage card (the phone's sd card) and in the real external storage card (the removable micro sd card).
My path of the phone's sd card (the one that you can't remove) is: /storage/emulated/0
.
My path of the micro sd card (the one that you can insert or remove) is: /storage/external_SD
.
Please note: When I use Environment.getExternalStorageDirectory().getPath()
i get: /storage/emulated/0
.
This is the path of the real internal storage card (the phone's sd card) and not the real external storage card (the removable micro sd card).
My problem is that I can write to the "real internal storage card" (the phone's sd card) but not to the "real external storage card" (the removable micro sd card). And I need to save files on the micro sd card.
I have tried the following things:
FileOutputStream fos;
1) fos = new FileOutputStream(System.getenv("SECONDARY_STORAGE") + "/test.txt");
2) fos = new FileOutputStream("/storage/external_SD/test.txt");
In both ways, I put the path of the micro sd card at the parameter of the FileOutputStream so it will save the file 'test.txt' on the micro sd card, but my app crashes and nothing happens!
In addition, I tried also to create a folder, but it didn't work.
However, when I put the path of the phone's sd card, it works!
And also folder creation works.
But I need to write files on the micro sd card.
Please note the following things before giving me an answer:
1) I put the following permissions in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
2) I had tried disconnecting the cable from my computer before I tried the app.
3) I tried disabling debugging mode.
4) I tried another micro sd card.
5) My smartphone is LG Optimus L7 (LG P714).
6) I have Android KitKat (4.4.2).
7) When I tried the app on another smartphone (Samsung Galaxy S3) it still crashed when I saved files on the micro sd card, but when I saved a folder it succeeded!
Does anyone know what is the problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…