我的目标是在外部SD卡上给定目录中不存在的文件中写入一些文本。不幸的是,我不能用我的android应用程序在目录中创建这样的文件。
我发现有几个代码示例对其他人有效,但它们不适用于我的应用程序。因此,任何建议都是非常感谢的。谢谢!
下面是我的相关代码:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>FileOutputStream fos ;
if( Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ) {
try {
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() + File.separator +
"givenDirectory" + File.separator + "newFile");
fos = new FileOutputStream(file);
String data = "some text";
fos.write(data.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}我在手机上使用Android-version 5.1.1。java类的提取是静态void的一部分,它只包含引用的代码。
问题:未创建文件
发布于 2016-09-14 05:42:41
https://stackoverflow.com/questions/39479379
复制相似问题