简体   繁体   中英

Unable to access public folders on Internal storage

I have an application that works fine on standard devices with either External storage partition or built-in/external SD card. But recently I switched to an Acer tablet for testing, and all file operations are broken on it.

The tablet has only Internal storage , on which the standard public directories are situated (Download, DCIM, etc). I have a storage library that checks whether External is available, if not - configures to use Internal. However, I have been totally unable to access the public directories, with or without the library. Here is my impressions:

  • An external partition is detected, Environment.getExternalDirectory() return some public folder, MEDIA_MOUNTED (read/write) status, but I can't write, read, create dir anything at all (read/write permissions in Manifest.xml set correctly); /sdcard is empty if accessed from some place, not from another, some voodoo symlink magic, I guess ...

  • On the other hand, when I switch to using the Internal storage , I can only write files to the application private folder (com/myapp/.../app_) /sdcard is inaccessible, although it is in the Internal, practically.

  • Tried to use Intents and ContentResolvers in order to let the Android OS itself decide how to acquire/give access to data and the public-internal dirs. Even if it worked, I would need to fully re-implement my FileUtils and FilePicker, just because of that specific device.(the app has worked correctly on several tablets, phones, emulators...)
  • adb shell from terminal gives me straightforward access to the public-internal directories; Android Device Monitor does not;

Any opinion is appreciated. Thanks.

Follow the below links for more details :

https://developer.android.com/training/basics/data-storage/files.html https://developer.android.com/guide/topics/data/data-storage.html#filesInternal

When saving a file to internal storage, you can acquire the appropriate directory as a File by calling one of two methods:

  • getFilesDir()
  • getCacheDir()

For example :

File file = new File(context.getFilesDir(), filename);

Alternatively, you can call openFileOutput() to get a FileOutputStream that writes to a file in your internal directory. For example, here's how to write some text to a file:

String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

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