【发布时间】:2015-02-27 07:52:24
【问题描述】:
我一直在寻找有关android文件写入的许多主题,但大多数人都想将文件写入android内部存储。其他想在外部SD卡上写入文件的人根本没有成功。我的情况非常相似,但我认为将文件写入外部 USB 是完全不同的情况。
我正在使用三星 Galaxy Note II 运行库存 TouchWiz 4.4.2 [未植根]。我的手机支持 micro-USB-OTG,我可以将我的 USB 挂载为 rwxrwx--x 而无需生根。我的 USB 的完整路径是 /storage/UsbDriveA。
我尝试使用 Environment.getExternalStorageDirectory() 来获取路径或直接使用路径(如上所述),但它们都没有成功。第一个返回内部存储路径,第二个返回“权限被拒绝”错误。我已经放了
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在 Android Manifest 中,所以我想知道为什么我的代码不起作用。
此外,我可以使用 Root Browser(无需 root 即可使用)和 Simple Browser 将任何内容写入我的 USB,因此我相信有办法做到这一点。
这是我的代码:
File file = new File(path.getAbsolutePath(), "test.txt");
// File file = new File("/storage/extSdCard","test.txt");
err = false;
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.print(get);
pw.flush();
pw.close();
f.close();
}catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "writing error",Toast.LENGTH_LONG).show();
err = true;
}
Log.i("File Path:", file.getPath());
提前致谢!!!
【问题讨论】:
标签: android android-external-storage writing