在运行时生成了FIleNotFoundException错误

时间:2013-03-23 13:02:02

标签: android file android-file

private void readFileFromSDCard() {
        File directory = Environment.getExternalStorageDirectory();

        File file = new File(directory+"/HomeActivityLogs");
        //file.getParentFile().mkdirs();

        if (!file.exists()) {

            FileWriter gpxwriter;
            try {
                System.out.println(" IN TRY Error");
                file.createNewFile();
                gpxwriter = new FileWriter(file);

                System.out.println(" file writer Error");
            BufferedWriter out = new BufferedWriter(gpxwriter);
            out.write("http://192.168.1.126/msfaws2_4/Service.asmx");
            System.out.println(" in url Error");
        ///    out.write("http://192.168.1.250/msfaws2_4/Service.asmx");
            out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                loginLog.appendLog("Exception in readFileFromSDCard() " + e.getMessage(),"MainActivity");
            }
        }

        BufferedReader reader = null;
        try {
            System.out.println("Error");
            reader = new BufferedReader(new FileReader(file));
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            Constant.URL = builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    loginLog.appendLog("Exception in readFileFromSDCard() " + e.getMessage(),"MainActivity");
                }
            }
        }
}

请帮我解决这个问题。它给出了一个文件名错误的文件 当数据临时存储然后从活动中检索时,它会从Web服务动态创建。

1 个答案:

答案 0 :(得分:0)

试试这个,在manifest.xml

中添加此权限
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />