简体   繁体   English

无法访问内部存储器上的公用文件夹

[英]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. 我有一个可以在带有External storage分区或内置/外部SD卡的标准设备上正常运行的应用程序。 But recently I switched to an Acer tablet for testing, and all file operations are broken on it. 但是最近我切换到Acer平板电脑进行测试,并且所有文件操作都被破坏了。

The tablet has only Internal storage , on which the standard public directories are situated (Download, DCIM, etc). 平板电脑只有Internal storage ,标准公共目录位于Internal storage (下载,DCIM等)。 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); 检测到外部分区, Environment.getExternalDirectory()返回一些公用文件夹,MEDIA_MOUNTED(读/写)状态,但我根本无法写,读,创建dir( Manifest.xml读/写权限已正确设置) ; /sdcard is empty if accessed from some place, not from another, some voodoo symlink magic, I guess ... / sdcard是空的,如果从某个地方访问,而不是从另一个地方访问,则是一些voodoo symlink魔术,我想...

  • 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. 另一方面,当我切换为使用Internal storage ,实际上只能将文件写入应用程序专用文件夹(com / myapp /.../ app_)/ sdcard,尽管该文件位于Internal storage

  • 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. 试图使用IntentsContentResolvers ,以便让Android OS自己决定如何获取/提供对数据和公共内部目录的访问。 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...) 即使它工作了,我也需要完全重新实现我的FileUtils和FilePicker,仅因为该特定设备而已(该应用程序已在数种平板电脑,手机,仿真器上正常运行...)
  • adb shell from terminal gives me straightforward access to the public-internal directories; 来自终端的adb shell使我可以直接访问公共内部目录; Android Device Monitor does not; Android Device Monitor不支持;

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 https://developer.android.com/training/basics/data-storage/files.html https://developer.android.com/guide/topics/data/data-storage.html#files内部

When saving a file to internal storage, you can acquire the appropriate directory as a File by calling one of two methods: 将文件保存到内部存储器时,可以通过调用以下两种方法之一来获取适当的目录作为文件:

  • getFilesDir() getFilesDir()
  • getCacheDir() 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. 另外,您可以调用openFileOutput()获得一个FileOutputStream,该文件将写入内部目录中的文件。 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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