0

I am trying to export some data on my app. Even though I set required permission on manifest and deleted build multiple times, it gives the same error. How can I fix it? FileNotFoundException: /storage/emulated/0/VocAppFile/output.txt (No such file or directory) The thing is there is no such folder like above in my phone. But /storage/emulated/0/ is the default directory.

My code is this:

String state = Environment.getExternalStorageState();
    if(Environment.MEDIA_MOUNTED.equals(state)){
        File Root = Environment.getExternalStorageDirectory();
        Log.d("Export to", Root.getAbsolutePath());
        File Dir = new File(Root.getAbsolutePath()+"/AppFile");
        if(!Dir.exists()){
            Dir.mkdir();
        }
        File file = new File(Dir,"output.txt");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            for(String[] d : data){
//data comes ready
                fos.write(d[0].getBytes());
                fos.write(d[1].getBytes());
            }
            fos.close();
            Toast.makeText(getApplicationContext(), "Data exported", 
Toast.LENGTH_SHORT).show();
        }catch (FileNotFoundException e){
            Log.wtf("My activity", "Error in exporting words");
            e.printStackTrace();
        }catch (IOException e){
            Log.wtf("My activity", "Error in exporting words");
            e.printStackTrace();
        }
Tolga
  • 116
  • 2
  • 12
  • `Dir.mkdir();`. Change that to `if(!Dir.mkdir()){Toast(...could not create directory...); return;}`. – greenapps Nov 04 '17 at 21:57
  • By the way: you did not react on my comment in your other post. Strange. – greenapps Nov 04 '17 at 22:00
  • I said I edited the the question but you did not reply. – Tolga Nov 04 '17 at 22:59
  • I changed the code as you suggested. It says cannot create a directory because !Dir.mkdir() is true. – Tolga Nov 04 '17 at 23:15
  • Yes indeed. The directory cannot be created. But you complained about a file that could not be written. Well in a directory that not exists thats difficult i think. – greenapps Nov 05 '17 at 09:40
  • `edited the the question but you did not reply.`. Yes, indeed. That was because i waited for all the info i had asked for. – greenapps Nov 05 '17 at 09:42
  • Thanks for the help anyways. Seeing directory failure helped me solve the issue. Proper error detection you got there :) – Tolga Nov 05 '17 at 11:11

2 Answers2

0

Put this line after File Dir = ...

New Line

file.createNewFile();

Final Code will be like this

    String state = Environment.getExternalStorageState();
        if(Environment.MEDIA_MOUNTED.equals(state)){
            File Root = Environment.getExternalStorageDirectory();
            Log.d("Export to", Root.getAbsolutePath());
            File Dir = new File(Root.getAbsolutePath()+"/AppFile");
            if(!Dir.exists()){
                Dir.mkdir();
            }
            File file = new File(Dir,"output.txt");
            file.createNewFile();
            try {
                FileOutputStream fos = new FileOutputStream(file);
                for(String[] d : data){
    //data comes ready
                    fos.write(d[0].getBytes());
                    fos.write(d[1].getBytes());
                }
                fos.close();
                Toast.makeText(getApplicationContext(), "Data exported", 
    Toast.LENGTH_SHORT).show();
            }catch (FileNotFoundException e){
                Log.wtf("My activity", "Error in exporting words");
                e.printStackTrace();
            }catch (IOException e){
                Log.wtf("My activity", "Error in exporting words");
                e.printStackTrace();
            }
Abdur Rahman
  • 894
  • 1
  • 11
  • 27
  • Nonsense. That line should NOT be added. – greenapps Nov 04 '17 at 21:58
  • @greenapps Maybe this line doesn't work but it make sense. And be as ethical as you can. This is not the platform to show people their limit you should help other there. If you have not the solution then shut your mouth why replying? – Abdur Rahman Nov 05 '17 at 13:03
0

I fixed the problem with following steps 1-2-4 of stackoverflow.com/a/41221852/5488468

Tolga
  • 116
  • 2
  • 12