【问题标题】:FileOutputStream not working with API level >= 22FileOutputStream 不适用于 API 级别 >= 22
【发布时间】:2015-12-31 10:45:17
【问题描述】:

我正在尝试写入 SDCARD 上的文件...我对模拟器和真实设备上 API lvl 22 下的这段代码没有任何问题。但是 API 级别更高,然后 22 它由于某种原因无法正常工作(我在模拟器上测试它,我没有可用于测试 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

【问题讨论】:

  • 您是否在 logcat 中的 Api 级别 >=22 上遇到错误?
  • logcat 完全没有错误...
  • 我更新了我的帖子。我意识到我之前使用的代码没有捕获 FileOutputStream 行的异常。现在我至少在 logcat 中有一个错误(文件由于某种原因不存在)...请检查它
  • 目标路径不存在,即某个中间目录。
  • 是的,好吧,在 API lvl 22 模拟器的路径总是:/storage/sdcard/smpcDir/smpcFile.txt ...不知道那个带有数字“/1608-2C08”的奇怪文件夹是什么,如果我我正在尝试使用 API 23 模拟器...

标签: java android


【解决方案1】:

我找到了解决方案HERE

在 Android api 级别 23 中,权限模型发生了变化。现在您需要在运行时请求权限。

然后我像THIS一样实现它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    相关资源
    最近更新 更多