android中的公共文本文件

时间:2016-10-31 12:21:15

标签: android filewriter

我有一个写文本文件的功能,它工作正常,文件是可见的,可以从手机的文件管理器打开但是当我将手机连接到电脑时我可以'看文件,有什么问题?

 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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

解决了它

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();
        }
    }
}