I am trying to write to a file, on SDCARD... I have no problem with this piece of code on emulator and on real device under API lvl 22. But with higher API level then 22 its not working for some reason (I was testing it on Emulator, I have no real device available to test on with higher then 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("");
Thats the line where it stops without an error (with API level >= 22):
OutputStream output = new FileOutputStream(file);
and immediatelly jumps to
stream.close();
What should be the problem?
UPDATE:
Ok, after putting a catch Exeption for FileOutputStream, I am getting this in 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