我正在创建一个xml
文件,我想在app
中保存我的数据。但我在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:
在我的清单文件中,我有权限
<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) {
}
};
注意
我知道有很多与之相关的问题,但我找不到更好的解决方案。
更新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();
}
}
});
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
您的文件是/ storage / emulated / 0filename,无法创建,使用&#39; /&#39;文件名和存储位置之间
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/" + filename);
file.createNewFile();
答案 1 :(得分:1)
您需要为File create
添加运行时权限将与文件相关的代码移动到单独的方法,并在授予权限时调用它。
例如:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0); //add this code in onCreate()
现在:
if(isPermissionGranted()){
do_your_work(); //create a method or write the whole code here
}
public boolean isPermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getContext(), "Permission granted", Toast.LENGTH_SHORT).show();
//do your work call method
} else {
Toast.makeText(getContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}