2017-01-22 93 views
0

這是我的代碼,當我運行它不存儲或生成任何文件在我的SD卡。我不明白我在做節目的錯誤道歉的錯誤,因爲IM初學者Sqlite數據到Android中的CSV文件

List<ClientModel> list = new ArrayList<ClientModel>(); //My Model 
     DatabaseHelper db = new DatabaseHelper(getApplicationContext()); //My DBHelper Class 
     list = db.getAllClientList(); //My Method to get all data from database 

     File exportDir = new File(Environment.getExternalStorageDirectory(), ""); // 

     if (!exportDir.exists()) 
     { 
      exportDir.mkdirs(); 
     } 

     File file = new File(exportDir, "Abc.csv"); //File Name 


     //writing data to a txt file 

     try { 

      file.createNewFile(); 
      CSVWriter csvWrite = new CSVWriter(new FileWriter(file)); //Writing data in csv file 
      for (int index = 0; index < list.size();) { 
       String arrStr[] = { String.valueOf(list.get(0))}; 
       csvWrite.writeNext(arrStr); 

      } 
      csvWrite.close(); 

      //But Above Code is not working for me 

     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
+0

你有沒有在manifest中寫入權限? –

+0

是的,我已經添加了 Cool

+0

什麼錯誤接收'catch'塊? – rafsanahmad007

回答

1

你需要檢查運行permission對於API 23:

試試這個:

if(isStoragePermissionGranted()){ 
     Save_file(); 
    } 

現在添加這個方法在你的活動中:

public void Save_file(){  
     File exportDir = new File(Environment.getExternalStorageDirectory(), ""); // 

       if (!exportDir.exists()) 
       { 
        exportDir.mkdirs(); 
       } 
       File file = new File(exportDir, "Abc.csv"); //File Name 
       //writing data to a txt file 
       try { 

        file.createNewFile(); 
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file)); //Writing data in csv file 
        for (int index = 0; index < list.size();) { 
         String arrStr[] = { String.valueOf(list.get(0))}; 
         csvWrite.writeNext(arrStr); 

        } 
        csvWrite.close(); 

        //But Above Code is not working for me 

       } catch (IOException e) { 

        e.printStackTrace(); 
       } 
      } 
     } 

public boolean isStoragePermissionGranted() { 
    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(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); 
      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) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){ 
     Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]); 
     //resume tasks needing this permission 
     Save_file(); 
    } 
} 
+0

嘗試你的代碼工作Thx人。我沒有每次檢查的API 23的權限 – Cool

+0

@Cool如果答案幫助..通過點擊勾號 – rafsanahmad007

+0

完成標記爲正確... – Cool

相關問題