简体   繁体   中英

How do I create a file in the “Internal Storage” folder (Primary External Storage) of my Samsung A51 Android 10?

I understand in current SDKs that external storage files for my app are created in the /data/data/"package" folder. However, I am integrating with another app that I have no control over and it reads from the "Internal Storage" (Which is the PRIMARY EXTERNAL STORAGE? AARG: .) folder on my Android 10 (R) phone. I've searched for 2 days now how to create a simple.csv file in the folder labeled "Internal Storage" (My Files->Internal Storage) and simply can't find a solution to getting the path to that location.

I'm setting my Android Studio to minSdkVersion at 16, complileSdkVersion to 28, so at least I can use some of the earlier methods for file management. I expect my app to run on phones/tablets at least 3 years old and newer. There is NO UI necessary (picker-as new doc suggests to use) to select a folder to save the file, just simply create a file there is all I need. I'm an old Java programmer but new to Android. Most posts point to deprecated functions we lose in >28 SDKs.

I understand in current SDKs that external storage files for my app are created in the /data/data/"package" folder.

No, that is internal storage .

However, I am integrating with another app that I have no control over and it reads from the "Internal Storage" (Which is the PRIMARY EXTERNAL STORAGE? AARG: :) folder on my Android 10 (R) phone

That app will need to be updated soon.

I've searched for 2 days now how to create a simple.csv file in the folder labeled "Internal Storage" (My Files->Internal Storage) and simply can't find a solution to getting the path to that location

For Android 10 and below, you can use Environment.getExternalStorageDirectory() for the root of external storage . Note that for Android 10 itself, you will need to add android:requestLegacyExternalStorage="true" in the <application> element.

On Android 11+, you can no longer write to the root of external storage using filesystem APIs, unless you hold MANAGE_EXTERNAL_STORAGE . That permission will require justification for use, if you plan on distributing your app via the Play Store (and perhaps elsewhere).

There is NO UI necessary (picker-as new doc suggests to use) to select a folder to save the file

The user may disagree with you. Google definitely disagrees with you. On Android 11, if you do not want your app to be banned from the Play Store, you will need to use ACTION_CREATE_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE and allow the user to decide where the user's file goes on the user's device.

Most posts point to deprecated functions we lose in >28 SDKs.

"Deprecated" in Android does not always mean "lose". It always means "there is another option that Google wishes for you to consider. So, for example, Environment.getExternalStorageDirectory() still exists, even in Android 11. It is just less useful, and Google would prefer that you use methods on StorageVolume instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM