我如何处理filenotfoundexception。我的应用程序在某些设备上崩溃但在其他设备上运行良是否有一个try catch来处理文件未找到的exeption或如何处理此错误。
03-31 12:25:49.477: E/MediaPlayer(28356): error: /storage/emulated/0/user_image_name.jpg: open failed: ENOENT (No such file or directory)
03-31 12:25:49.477: E/MediaPlayer(28356): java.io.FileNotFoundException: /storage/emulated/0/user_image_name.jpg: open failed: ENOENT (No such file or directory)
03-31 12:25:49.477: E/MediaPlayer(28356): at libcore.io.IoBridge.open(IoBridge.java:416)
03-31 12:25:49.477: E/MediaPlayer(28356): at java.io.FileInputStream.<init>(FileInputStream.java:78)
03-31 12:25:49.477: E/MediaPlayer(28356): at name.secondname.app.utils.HttpRequest.doFileUpload(HttpRequest.java:2949)
03-31 12:25:49.477: E/MediaPlayer(28356): at name.secondname.app.moncompte.Editer$uploadImageLoadingTask.doInBackground(Editer.java:1099)
03-31 12:25:49.477: E/MediaPlayer(28356): at name.secondname.app.moncompte.Editer$uploadImageLoadingTask.doInBackground(Editer.java:1)
03-31 12:25:49.477: E/MediaPlayer(28356): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-31 12:25:49.477: E/MediaPlayer(28356): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-31 12:25:49.477: E/MediaPlayer(28356): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-31 12:25:49.477: E/MediaPlayer(28356): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-31 12:25:49.477: E/MediaPlayer(28356): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-31 12:25:49.477: E/MediaPlayer(28356): at java.lang.Thread.run(Thread.java:856)
03-31 12:25:49.477: E/MediaPlayer(28356): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
03-31 12:25:49.477: E/MediaPlayer(28356): at libcore.io.Posix.open(Native Method)
03-31 12:25:49.477: E/MediaPlayer(28356): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
03-31 12:25:49.477: E/MediaPlayer(28356): at libcore.io.IoBridge.open(IoBridge.java:400)
03-31 12:25:49.477: E/MediaPlayer(28356): ... 10 more
03-31 12:25:49.497: E/AndroidRuntime(28356): FATAL EXCEPTION: AsyncTask #5
03-31 12:25:49.497: E/AndroidRuntime(28356): java.lang.RuntimeException: An error occured while executing doInBackground()
03-31 12:25:49.497: E/AndroidRuntime(28356): at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-31 12:25:49.497: E/AndroidRuntime(28356): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-31 12:25:49.497: E/AndroidRuntime(28356): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-31 12:25:49.497: E/AndroidRuntime(28356): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-31 12:25:49.497: E/AndroidRuntime(28356): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-31 12:25:49.497: E/AndroidRuntime(28356): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-31 12:25:49.497: E/AndroidRuntime(28356): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-31 12:25:49.497: E/AndroidRuntime(28356): at java.lang.Thread.run(Thread.java:856)
03-31 12:25:49.497: E/AndroidRuntime(28356): Caused by: java.lang.NullPointerException
03-31 12:25:49.497: E/AndroidRuntime(28356): at name.secondname.app.utils.HttpRequest.doFileUpload(HttpRequest.java:3006)
03-31 12:25:49.497: E/AndroidRuntime(28356): at name.secondname.app.moncompte.Editer$uploadImageLoadingTask.doInBackground(Editer.java:1099)
03-31 12:25:49.497: E/AndroidRuntime(28356): at name.secondname.app.moncompte.Editer$uploadImageLoadingTask.doInBackground(Editer.java:1)
03-31 12:25:49.497: E/AndroidRuntime(28356): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-31 12:25:49.497: E/AndroidRuntime(28356): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-31 12:25:49.497: E/AndroidRuntime(28356): ... 4 more
答案 0 :(得分:1)
是的,在文件打开逻辑周围放置一个try-catch块,并在捕获异常时处理异常。
try {
// File related operations
} catch (Exception e) {
// Handle the exception here
}