我正在創建一個xml
文件,其中我想從我的app
保存我的數據。但我得到以下例外logcat
無法在android中創建XML文件
java.io.IOException: open failed: EACCES (Permission denied)
03-02 12:08:40.352 16419-16419/com.example.accurat.application W/System.err: at java.io.File.createNewFile(File.java:939)
03-02 12:08:40.352 16419-16419/com.example.accurat.application W/System.err: at com.example.accurat.application.MainActivity$4.onPermissionGranted(MainActivity.java:192)
03-02 12:08:40.352 16419-16419/com.example.accurat.application W/System.err: at com.gun0912.tedpermission.TedInstance.onPermissionResult(TedInstance.java:65)
03-02 12:08:40.352 16419-16419/com.example.accurat.application W/System.err: at java.lang.reflect.Method.invoke(Native Method)
03-02 12:08:40.352 16419-16419/com.example.accurat.application W/System.err: at com.squareup.otto.EventHandler.handleEvent(EventHandler.java:89)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at com.squareup.otto.Bus.dispatch(Bus.java:385)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:368)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at com.squareup.otto.Bus.post(Bus.java:337)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at com.gun0912.tedpermission.busevent.TedBusProvider.post(TedBusProvider.java:49)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at com.gun0912.tedpermission.TedPermissionActivity.permissionGranted(TedPermissionActivity.java:123)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at com.gun0912.tedpermission.TedPermissionActivity.checkPermissions(TedPermissionActivity.java:190)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at com.gun0912.tedpermission.TedPermissionActivity.onCreate(TedPermissionActivity.java:65)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at android.app.Activity.performCreate(Activity.java:6270)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2506)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at android.app.ActivityThread.-wrap11(ActivityThread.java)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at android.os.Looper.loop(Looper.java:148)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5491)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at java.lang.reflect.Method.invoke(Native Method)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at libcore.io.Posix.open(Native Method)
03-02 12:08:40.353 16419-16419/com.example.accurat.application W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
03-02 12:08:40.354 16419-16419/com.example.accurat.application W/System.err: at java.io.File.createNewFile(File.java:932)
03-02 12:08:40.354 16419-16419/com.example.accurat.application W/System.err:
在我的清單文件我有權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
下面是我的代碼部分,其中我得到錯誤
final PermissionListener ListenerSaveData = new PermissionListener() {
@Override
public void onPermissionGranted() {
if(refr_no == "Select a reference number" || meter_type == "Select Meter Type" || Latitude == " " || Longitude == ""
|| site_status == "Select Site Status" || comm_status == "" || pole_type == "Select pole type" || DateTime == "")
{
Toast.makeText(MainActivity.this, " Data not saved.... you must be missing some thing.. Please check!!! " , Toast.LENGTH_LONG).show();
}else {
int selectedRadioButton = rg_meter_placement.getCheckedRadioButtonId();
rb_meter_placement = (RadioButton)findViewById(selectedRadioButton);
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateTime = df.format(c.getTime());
comm_status = String.valueOf(comment.getText());
try {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + filename);
file.createNewFile();
FileOutputStream fileos = new FileOutputStream(file);
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8", true);
xmlSerializer.startTag(null,"record");
xmlSerializer.startTag(null,"ref_no");
xmlSerializer.text(refr_no);
xmlSerializer.endTag(null,"refr_no");
xmlSerializer.startTag(null,"meter_type");
xmlSerializer.text(meter_type);
xmlSerializer.endTag(null,"meter_type");
xmlSerializer.startTag(null,"lat");
xmlSerializer.text(Latitude);
xmlSerializer.endTag(null,"lat");
xmlSerializer.startTag(null,"long");
xmlSerializer.text(Longitude);
xmlSerializer.endTag(null,"long");
xmlSerializer.startTag(null,"site_status");
xmlSerializer.text(site_status);
xmlSerializer.endTag(null,"site_status");
xmlSerializer.startTag(null,"communication_status");
xmlSerializer.text(comm_status);
xmlSerializer.endTag(null,"communication_status");
xmlSerializer.startTag(null, "pole_type");
xmlSerializer.text(pole_type);
xmlSerializer.endTag(null,"pole_type");
xmlSerializer.startTag(null,"meter_placement");
xmlSerializer.text(String.valueOf(rb_meter_placement));
xmlSerializer.endTag(null,"meter_placement");
xmlSerializer.startTag(null,"date_time");
xmlSerializer.text(DateTime);
xmlSerializer.endTag(null,"date_time");
xmlSerializer.endTag(null,"record");
xmlSerializer.endDocument();
xmlSerializer.flush();
String dataWrite = writer.toString();
fileos.write(dataWrite.getBytes());
fileos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onPermissionDenied(ArrayList<String> deniedPermissions) {
}
};
注
我知道th有很多問題都與它有關,但我找不到更好的解決方案。
更新1
對於允許我使用ted
如下
btn_save_data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(refr_no == "Select a reference number" || meter_type == "Select Meter Type" || Latitude == " " || Longitude == ""
|| site_status == "Select Site Status" || comm_status == "" || pole_type == "Select pole type" || DateTime == "")
{
Toast.makeText(MainActivity.this, " Data not saved.... you must be missing some thing.. Please check!!! " , Toast.LENGTH_LONG).show();
}else {
new TedPermission(MainActivity.this)
.setPermissionListener(ListenerSaveData)
.setRationaleMessage("This activity will need your permission to save file ")
.setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.check();
}
}
});
任何幫助將高度讚賞。
https://developer.android.com/training/permissions/requesting.html –
@ cricket_007我已經有許可請求 – faisal1208
@ cricket_007請看我更新的代碼 – faisal1208