faisal1208 faisal1208 -4 years ago 152
Android Question

Unable to create XML file in android

I am creating an

xml
file in which i want to save my data from my
app
. But i am getting the below exception in
logcat


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:


In my manifest file i have the permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


Below is my code part in which i am getting error

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) {

}
};


Note

I know there are a lot questions asked related to it but i couldn't find any better solution.

Update 1

For permission i am using
ted
as below

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();
}

}
});


Any help would be highly appreciated.

Answer Source

Your file is /storage/emulated/0filename which could not be possible to create, use '/' between file name and storage location

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/" + filename);
                          file.createNewFile();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download