3

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
David Toth
  • 85
  • 9
  • 1
    are you getting an error on Api level >=22 in logcat? – Aegis Dec 31 '15 at 10:48
  • No error in logcat at all... – David Toth Dec 31 '15 at 11:05
  • I have updated my post. I have realized that the code that I was using before was not catching exeption for FileOutputStream line. Now I have an error in logcat at least (the file does not exists for some reason)... check it please – David Toth Dec 31 '15 at 11:18
  • The target *path* doesn't exist, that is to say some intermediate directory. – user207421 Dec 31 '15 at 11:26
  • Yess, well, under API lvl 22 emulator's path is always: /storage/sdcard/smpcDir/smpcFile.txt ... Dunno whats that wierd folder with numbers "/1608-2C08", if I am trying it with API 23 emulator... – David Toth Dec 31 '15 at 12:13

1 Answers1

0

I have found the solution HERE

In Android api level 23 the permission model changed. Now you need to request permissions at runtime.

Then I implemented it like THIS

Community
  • 1
  • 1
David Toth
  • 85
  • 9