1

I have copied the method from this question and modified a bit to use in my project. I have noticed, that when I call the method createNewFile() , the app ignores the rest of my method. How can i fix this?

  public void exportDatabase(){
    try{
        File dbFile=getDatabasePath("Glukozko.db");
        File exportDir = new File(Environment.getExternalStorageDirectory(), "");
        if (!exportDir.exists()){
            exportDir.mkdirs();
        }
        File file = new File(exportDir, "meritve.csv");
        file.createNewFile();  //from this point on, the method is ignored
        PrintWriter csvWrite = new PrintWriter(new FileWriter(file));
        Cursor meritve=db.vrniMeritve();
        if(meritve.getCount()==0){
            Toast.makeText(MainActivity.this,"No data availible",Toast.LENGTH_SHORT).show();
        }else{
            while (meritve.moveToNext()){
                String vrstica=meritve.getString(1)+";"+meritve.getString(2)+";"+meritve.getString(3)+";"+meritve.getString(4)+";"+meritve.getString(5)+";"+meritve.getString(6);
                System.out.println(vrstica);
            }
        }
    }catch (Exception e){

    }
}

I have found the solution.

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

I had to put this in the manifset file!

Community
  • 1
  • 1

1 Answers1

2
file.createNewFile();  //from this point on, the method is ignored

It has thrown an exception!

You then proceed to squash the exception ... by catching it and doing nothing in the catch block.

If you printed the exception's stacktrace instead of squashing it, you would probably see a big clue about what has caused the exception. Once you know what causes it you can then figure out how to fix it.

Also the file is not created

Yup. The method call failed.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216