Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

android - Can't access the secondary storage (the external removable micro sd card)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

That's been the case since Android 4.4, released about four years ago.

But I need to write files on the micro sd card.

You can use getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs() on Context. Note the plural form of these method names. If these return 2+ items, the second and subsequent ones are locations on removable storage, unique to your app, that you can read from and write to.

Alternatively, use the Storage Access Framework and let the user choose where to put the content, where you use the resulting Uri with ContentResolver and openOuptutStream() to write to that location. This would allow the user to work with external storage, removable storage, plus locations supported by third-party apps (Google Drive, SMB/CIFS file servers, etc.).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...