全部產品
Search
文件中心

Mobile Testing - Deprecated:java_io_FileNotFoundException

更新時間:Jul 13, 2024

問題分析

檔案未找到或無許可權時拋出此類異常。

解決方案

檔案未找到或無許可權時,請確認檔案路徑檔案存在,添加所需存取權限,確保檔案存在的情況下進行讀寫,並增加try catch塊做異常處理。

程式碼範例

增加讀寫外部儲存上的檔案許可權:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

建立檔案,增加讀寫權限並catch exception。

File extDir = Environment.getExternalStorageDirectory();
String filename = "file.txt";
File fullFilename = new File(extDir, filename);
try {
if(!fullFilename.exists()){
        fullFilename.createNewFile();
        fullFilename.setWritable(Boolean.TRUE);
                // 寫檔案操作
    }
    else{
        // 讀檔案操作
    }
} catch (IOException e) {
    e.printStackTrace();
}

樣本:

java.io.FileNotFoundException: /proc/stprof/status: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:496)
    at java.io.FileInputStream.<init>(FileInputStream.java:76)
    at java.io.FileInputStream.<init>(FileInputStream.java:103)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5692)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
    at libcore.io.IoBridge.open(IoBridge.java:482)
    ... 10 more

異常導致應用無法啟動。日誌如下:

10-20 09:43:08.073   820   820 E WifiTrafficPoller: TRAFFIC_STATS_POLL true Token 1138 num clients 12
10-20 09:43:08.074   820   820 E WifiTrafficPoller:  packet count Tx=237196 Rx=493756
10-20 09:43:08.365   820   842 W BroadcastQueue: Skipping deliver [background] BroadcastRecord{9a089ad u-1 android.net.conn.CONNECTIVITY_CHANGE} to ReceiverList{2c1b84e2 23189 (unknown name)/2000/u-1 remote:2663fdd7}: process crashing
10-20 09:43:08.401 23079 23079 E ActivityThread: mtprof entry can not be found
10-20 09:43:08.401 23079 23079 E ActivityThread: java.io.FileNotFoundException: /proc/mtprof/status: open failed: ENOENT (No such file or directory)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at libcore.io.IoBridge.open(IoBridge.java:496)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at java.io.FileInputStream.<init>(FileInputStream.java:76)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at java.io.FileInputStream.<init>(FileInputStream.java:103)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at android.os.Handler.dispatchMessage(Handler.java:111)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at android.os.Looper.loop(Looper.java:194)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at android.app.ActivityThread.main(ActivityThread.java:5692)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at java.lang.reflect.Method.invoke(Native Method)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at java.lang.reflect.Method.invoke(Method.java:372)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
10-20 09:43:08.401 23079 23079 E ActivityThread: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at libcore.io.Posix.open(Native Method)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
10-20 09:43:08.401 23079 23079 E ActivityThread:     at libcore.io.IoBridge.open(IoBridge.java:482)
10-20 09:43:08.401 23079 23079 E ActivityThread:     ... 10 more
10-20 09:43:08.415 23079 23079 W Launcher: setApplicationContext called twice! 
10-20 09:43:08.422 23079 23079 E Fatal exception: :java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.io.IOException: broken file descriptor
10-20 09:43:08.422 23079 23079 W System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.io.IOException: broken file descriptor
10-20 09:43:08.422 23079 23079 W System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)
10-20 09:43:08.422 23079 23079 W System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
10-20 09:43:08.422 23079 23079 W System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:178)
10-20 09:43:08.423 23079 23079 W System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
10-20 09:43:08.423 23079 23079 W System.err:     at android.os.Handler.dispatchMessage(Handler.java:111)
10-20 09:43:08.423 23079 23079 W System.err:     at android.os.Looper.loop(Looper.java:194)
10-20 09:43:08.423 23079 23079 W System.err:     at android.app.ActivityThread.main(ActivityThread.java:5692)
10-20 09:43:08.423 23079 23079 W System.err:     at java.lang.reflect.Method.invoke(Native Method)
10-20 09:43:08.423 23079 23079 W System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
10-20 09:43:08.423 23079 23079 W System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
10-20 09:43:08.423 23079 23079 W System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
10-20 09:43:08.423 23079 23079 W System.err: Caused by: java.io.IOException: broken file descriptor
10-20 09:43:08.423 23079 23079 W System.err:     at android.graphics.BitmapFactory.nativeDecodeFileDescriptor(Native Method)
10-20 09:43:08.423 23079 23079 W System.err:     at android.graphics.BitmapFactory.decodeFileDescriptor(BitmapFactory.java:714)
10-20 09:43:08.423 23079 23079 W System.err:     at android.app.WallpaperManager$Globals.getCurrentWallpaperLocked(WallpaperManager.java:308)
10-20 09:43:08.423 23079 23079 W System.err:     at android.app.WallpaperManager$Globals.peekWallpaperBitmap(WallpaperManager.java:270)
10-20 09:43:08.423 23079 23079 W System.err:     at android.app.WallpaperManager.getDrawable(WallpaperManager.java:413)

更換手機案頭應用布局介面時調用native方法decodeFileDescriptor讀取檔案/proc/stprof/status拋出了異常未捕獲導致進程崩潰,建議按照樣本一判斷檔案是否存在並對異常進行捕獲處理。

參考文獻