我正在尝试写入一个文件,在SDCARD上...我在模拟器和API lvl 22下的真实设备上的这段代码没有问题。但是如果API级别更高那么22它因某些原因无法工作(我在Emulator上测试它,我没有真正的设备可用于测试更高的API lvl 22)...
Log.d(SmpcWidget.DEBUG_TAG, "Ext. storage readable: " + isExternalStorageReadable());
Log.d(SmpcWidget.DEBUG_TAG, "Ext. storage writable: " + isExternalStorageWritable());
String dir = Environment.getExternalStorageDirectory()+File.separator+"smpcDir";
//create folder
File folder = new File(dir); //folder name
folder.mkdirs();
//create file
File file = new File(dir, "smpcFile.txt");
//path += "testlab.txt";
try {
OutputStream output = new FileOutputStream(file);
try {
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
int read;
while ((read = stream.read(buffer)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
} finally {
output.close();
}
} catch (Exception e) {
e.printStackTrace(); // handle exception, define IOException and others
}
} catch (Exception e) { //UPDATE: Edited the code to catch exeption for FileOutputStream
e.printStackTrace();
} finally {
stream.close();
}
return new String("");
停止的行没有错误(API级别> = 22):
OutputStream output = new FileOutputStream(file);
立即跳转到
stream.close();
应该是什么问题?
更新:
好的,在为FileOutputStream添加了一个catch Exeption后,我在logcat中得到了这个:
12-31 12:11:48.814 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ java.io.FileNotFoundException: /storage/1608-2C08/smpcDir/smpcFile.txt: open failed: ENOENT (No such file or directory)
12-31 12:11:48.815 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:452)
12-31 12:11:48.815 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
12-31 12:11:48.815 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
12-31 12:11:48.816 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at com.smpc.davidtoth.smpc.UpdateWidgetService.readIt(UpdateWidgetService.java:321)
12-31 12:11:48.816 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at com.smpc.davidtoth.smpc.UpdateWidgetService.downloadUrl(UpdateWidgetService.java:257)
12-31 12:11:48.817 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at com.smpc.davidtoth.smpc.UpdateWidgetService.access$100(UpdateWidgetService.java:47)
12-31 12:11:48.817 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at com.smpc.davidtoth.smpc.UpdateWidgetService$DownloadWebpageTask.doInBackground(UpdateWidgetService.java:211)
12-31 12:11:48.818 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at com.smpc.davidtoth.smpc.UpdateWidgetService$DownloadWebpageTask.doInBackground(UpdateWidgetService.java:196)
12-31 12:11:48.818 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:295)
12-31 12:11:48.818 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-31 12:11:48.819 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
12-31 12:11:48.819 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
12-31 12:11:48.819 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
12-31 12:11:48.820 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
12-31 12:11:48.820 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
12-31 12:11:48.909 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at libcore.io.Posix.open(Native Method)
12-31 12:11:48.912 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
12-31 12:11:48.913 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:438)
12-31 12:11:48.913 17218-17235/com.smpc.davidtoth.smpc W/System.err﹕ ... 13 more