0

令人讨厌的是,我让下面的代码创建并写入 sdcard 上的文件,然后继续开发更多代码。但是我必须改变一些东西,因为现在它不起作用。

这是漫长而烦人的一天,所以我想知道是否有人可以指出我所犯的简单错误。

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy / hh-mm-ss");
Date curDate = new Date();
String stringDate = sdf.format(curDate);
String resultLogFile = "logFile " + stringDate;

File newFile = new File("sdcard/" + (resultLogFile));
if (!newFile.exists()) {
    try {
        newFile.createNewFile();
    }
    catch (IOException e) {
        e.printStackTrace();
    }

}
try {
    BufferedWriter buf = new BufferedWriter(new FileWriter(newFile, true));
    buf.append(writeToFileString);
    buf.newLine();
    buf.close();
} catch (IOException e) {
    e.printStackTrace();
}

这是控制台:

09-19 17:58:16.270: W/System.err(10411): java.io.IOException: open failed: ENOENT (No such file or directory)
09-19 17:58:16.275: W/System.err(10411):    at java.io.File.createNewFile(File.java:940)
09-19 17:58:16.275: W/System.err(10411):    at android.Maps.GeneticAlgorithm3.shufflePerm3(GeneticAlgorithm3.java:192)
09-19 17:58:16.275: W/System.err(10411):    at android.Maps.HomeScreen$6.onClick(HomeScreen.java:334)
09-19 17:58:16.275: W/System.err(10411):    at android.view.View.performClick(View.java:4084)
09-19 17:58:16.275: W/System.err(10411):    at android.view.View$PerformClick.run(View.java:16966)
09-19 17:58:16.275: W/System.err(10411):    at android.os.Handler.handleCallback(Handler.java:615)
09-19 17:58:16.275: W/System.err(10411):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-19 17:58:16.275: W/System.err(10411):    at android.os.Looper.loop(Looper.java:137)
09-19 17:58:16.275: W/System.err(10411):    at android.app.ActivityThread.main(ActivityThread.java:4896)
4

4 回答 4

1

尝试使用:

File newFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + (resultLogFile));

对于时间戳,您可以使用以下方式获取它,然后将其添加到名称的末尾:

public String date() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");  
        df.setTimeZone(TimeZone.getTimeZone("PST"));  
        return df.format(new Date());
}

此外,请确保您没有通过 USB 将 SD 卡安装到您的计算机等设备上,因为这会使其在安装时对应用程序不可用。

于 2012-09-19T17:18:37.833 回答
1

您真的应该使用Environment.getExternalStorageDirectory()来获取根 SD 卡位置。

于 2012-09-19T17:15:53.203 回答
0

SD 卡位置取决于制造商。当您使用 IDE 的手机时,您可以选择 USB 大容量存储和无,用于 USB 连接。如果您选择 USB 大容量存储而不是 SD 卡,则它不适用于您的应用程序 :)

我已经使用示例代码来检查它:

boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // We can read and write the media
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            // We can only read the media
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            // Something else is wrong. It may be one of many other states, but
            // all we need
            // to know is we can neither read nor write
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }

        if (mExternalStorageAvailable && mExternalStorageWriteable) {
            doWriteForExternalStorage();
        } else {
            Log.d(TAG, "mExternalStorageAvailable: " + mExternalStorageAvailable + ", mExternalStorageWriteable: " + mExternalStorageWriteable + " it is Connected to PC now?");
        }

@SuppressWarnings("unused")
    private void doWriteForExternalStorage() {
        // TODO Auto-generated method stub
        File extDir = Environment.getExternalStorageDirectory();
        Log.d(TAG, "extDir:" + extDir.getAbsolutePath());
        if (extDir.isDirectory() && extDir.canWrite()) {
            File fileData = new File(extDir, "mydata.txt");
            Log.d(TAG, "want to create file: " + fileData.toString());
            FileOutputStream fos = null;
            try {
                boolean append = true;
                fos = new FileOutputStream(fileData, append);
                BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);// 8kbyte
                                                                                // buff,
                                                                                // it
                                                                                // should
                                                                                // be
                                                                                // plenty

                StringBuilder sb = new StringBuilder("\n");
                List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(100);

                if (runningTasks != null) {
                    for (RunningTaskInfo runningTask : runningTasks) {
                        sb.append("runningTask: ").append(runningTask.baseActivity.getPackageName()).append(", ").append(runningTask.baseActivity.getClassName());
                    }
                } else {
                    sb.append("No running tasks");
                }
                byte[] data = sb.toString().getBytes();
                bos.write(data);
                bos.flush();


                             bos.close();
            fos = null;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null ) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

记录服务

于 2012-09-19T17:17:09.467 回答
0

我想你应该有"/sdcard/"而不是"sdcard/".

于 2012-09-19T17:14:18.427 回答