【发布时间】:2017-07-24 14:33:48
【问题描述】:
我无法在设备的 /tmp 位置创建文件。我认为这是一些许可问题。你能帮我么..? 在我的 android 清单中,我包含以下权限:-
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
我的活动代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
try{
write();
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(tv.getText()+" "+Register());
//System.out.println("The value of retval is"+nativeinterface());
}catch (Exception e){e.printStackTrace();}
}
public Boolean write(){
try {
String fcontent = "Hello world!!";
String fpath = "/tmp/abc.txt";
File file = new File(fpath);
// If file does not exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fcontent);
bw.close();
Log.d("Suceess","Sucess");
return true;
} catch (IOException e) {
System.out.println("The File Not Created");
e.printStackTrace();
return false;
}
}
}
我的日志如下:
W/System.err: java.io.IOException: Permission denied
W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
W/System.err: at java.io.File.createNewFile(File.java:948)
请帮我解决这个问题...
【问题讨论】:
-
我真的对您是否要关闭
fw、bw或两者都关闭(C# 是我现在的主要语言,我在 Xamarin.Android 中进行大部分 Android 开发)。