我正在尝试将文本保存到android上的内部存储(世界可读)。但是,文件未保存(文件夹也未创建)。
我的期望: - 名称'20170323_003033'的文件保存在内部存储中的'... / android / data / my.package.name /'
会发生什么: - 文件和目录my.package.name都不存在
我通过检查内部和外部存储中的../android/data文件夹以及运行搜索部分文件名'20170323'来验证文件/目录不存在。
相关代码:
private void saveInternalFile() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
String coords;
FileOutputStream outputStream;
if (location != null) {
coords = GpxUtils.getDMS(location.getLatitude(), GpxUtils.LATITUDE) + GpxUtils.getDMS(location.getLongitude(), GpxUtils.LONGITUDE);
try {
outputStream = openFileOutput(timeStamp, Context.MODE_WORLD_READABLE);
outputStream.write(coords.getBytes());
outputStream.close();
Toast toast = Toast.makeText(MainActivity.this, "Saved as " + timeStamp, Toast.LENGTH_SHORT);
toast.show();
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
} else {
Toast toast = Toast.makeText(MainActivity.this, "Unable to save", Toast.LENGTH_SHORT);
toast.show();
}
}
正如您所看到的,在捕获IOException时,我同时执行了printStackTrace
和System.exit(0)
。由于我既没有收到堆栈跟踪也没有退出应用程序,我很确定try块中的代码成功运行。吐司表现得如预期的那样。
Logcat没有显示触发此方法的按钮的onclick错误(显示详细日志):
03-23 01:44:38.376 11842-11842/io.ningyuan.gpslogger D/ViewRootImpl: ViewPostImeInputStage processPointer 0
03-23 01:44:38.506 11842-11842/io.ningyuan.gpslogger D/ViewRootImpl: ViewPostImeInputStage processPointer 1
03-23 01:44:38.576 11842-11842/io.ningyuan.gpslogger D/ViewRootImpl: #1 mView = android.widget.LinearLayout{4fd08dd V.E...... ......I. 0,0-0,0}
03-23 01:44:38.626 11842-11934/io.ningyuan.gpslogger D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [763x176]-format:1
03-23 01:44:38.626 11842-11842/io.ningyuan.gpslogger W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-23 01:44:38.666 11842-11934/io.ningyuan.gpslogger V/RenderScript: 0x7f825f4000 Launching thread(s), CPUs 8
03-23 01:44:38.676 11842-11842/io.ningyuan.gpslogger D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
03-23 01:44:40.556 11842-11842/io.ningyuan.gpslogger D/ViewRootImpl: #3 mView = null
03-23 01:44:40.576 11842-11842/io.ningyuan.gpslogger D/ViewRootImpl: #1 mView = android.widget.LinearLayout{5e4c52 V.E...... ......I. 0,0-0,0}
03-23 01:44:40.606 11842-11934/io.ningyuan.gpslogger D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [599x176]-format:1
03-23 01:44:40.606 11842-11842/io.ningyuan.gpslogger D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
03-23 01:44:40.616 11842-11842/io.ningyuan.gpslogger W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-23 01:44:42.576 11842-11842/io.ningyuan.gpslogger D/ViewRootImpl: #3 mView = null