0

I have a function to write a text file and it's working fine, the file is visible and can be opened from the file manager of the phone but when I connect the phone to the computer I can't see the file, what is the problem?

 public static void writeToFile(String sBody, Activity activity) {

    File directoryFile = new File(Environment.getExternalStorageDirectory()  +"/platefinder");//ApiCrypter.DIRECTORY);
    File file = new File(directoryFile, "plates_notes.txt");
    if(file.exists()){
        file.delete();
        PlateFinderDbAdapter datasource = new PlateFinderDbAdapter(activity);
        datasource.open();
        String s = datasource.getStringNotes();
        writeToFile(s, activity);
        datasource.close();
    }else{
        try {
            FileWriter writer = new FileWriter(file,true);
            writer.append(sBody);
            writer.flush();
            writer.close();
            Toast.makeText(activity, "تم تصدير الملف", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(activity, "حصل خطأ", Toast.LENGTH_SHORT).show();
        }
    }
}
eshteghel company
  • 471
  • 1
  • 7
  • 22

1 Answers1

0

Solved it

public static void writeToFile(String sBody, Activity activity) {

    File directoryFile = new File(Environment.getExternalStorageDirectory()  +"/platefinder");//ApiCrypter.DIRECTORY);
    File file = new File(directoryFile, "plates_notes.txt");
    if(file.exists()){
        file.delete();
        PlateFinderDbAdapter datasource = new PlateFinderDbAdapter(activity);
        datasource.open();
        String s = datasource.getStringNotes();
        writeToFile(s, activity);
        datasource.close();
    }else{
        try {
            FileWriter writer = new FileWriter(file,true);
            writer.append(sBody);
            writer.flush();
            writer.close();
            MediaScannerConnection.scanFile(activity,
                    new String[] { file.toString() },
                    null,
                    null);
            Toast.makeText(activity, "تم تصدير الملف", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(activity, "حصل خطأ", Toast.LENGTH_SHORT).show();
        }
    }
}
eshteghel company
  • 471
  • 1
  • 7
  • 22