2017-03-02 130 views
0

我正在創建一個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(); 
       } 

     } 
    }); 

任何幫助將高度讚賞。

+1

https://developer.android.com/training/permissions/requesting.html –

+0

@ cricket_007我已經有許可請求 – faisal1208

+0

@ cricket_007請看我更新的代碼 – faisal1208

回答

1

您的文件/存儲/模擬/ 0filename這可能是不可能的創建,文件名和存儲位置之間用「/」

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/" + filename); 
          file.createNewFile(); 
+0

我應該在哪裏找到我的手機中的文件通過連接到我的系統? – faisal1208

+0

此代碼snipset是從您的onPermissionGranted方法只是更新它,它會正常工作。 文件將在你的父存儲中,因爲你沒有提到它的任何目錄 –

+0

是的,它的工作,我可以看到我的文件,但是當我打開它,它說無法打開文件,我無法找到文件時我通過我的系統連接它 – faisal1208

1

你的需要添加運行許可文件創建

將文件相關代碼移至單獨的方法,並在授予權限時調用它。

例如:

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 
    } 
} 
+0

看來他實現了運行時權限 –

+0

看到錯誤的第一行:'java.io.IOException:打開失敗:EACCES(Permission denied)'並且認爲op的執行是對還是錯 – rafsanahmad007

+0

我看到了,但他說他做得對! IDK! –