Android:无法访问存储在SD卡中的文件

时间:2014-02-20 01:05:23

标签: android file-io

我正在开发一个应用程序,它可以读取存储在SD卡中的.txt文件(内部/数据/本地)。我遇到的问题是,它似乎可以访问文件路径,但它无法访问文件本身(尽管它具有读写权限),它会抛出FileNotFoundException。 在这里,我给你留下代码和日志:

String ruta="/data/local/textojson.txt";
System.out.println(ruta);
String cadena="", Jsontx="";            

File sd = Environment.getExternalStorageDirectory();
File f = new File(sd.getAbsolutePath(), ruta);
System.out.println(f);

BufferedReader entrada;
try 
{
    entrada = new BufferedReader(new FileReader(f));
    System.out.println(entrada);

    try 
    {
        while((cadena=entrada.readLine())!=null)
        {   

            Jsontx=cadena;
        }
    } catch (IOException e) 
    {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }
} catch (FileNotFoundException e1) 
{
// TODO Auto-generated catch block
    e1.printStackTrace();
} 

我已将清单的READ_EXTERNAL_STORAGE权限添加到该目录中,该文件确实存在。

02-04 19:26:40.535: E/Trace(20167): error opening trace file: No such file or directory (2)
02-04 19:26:41.055: I/System.out(20167): /data/local/textojson.txt
02-04 19:26:41.066: I/System.out(20167): /mnt/sdcard/data/local/textojson.txt
02-04 19:26:41.066: W/System.err(20167): java.io.FileNotFoundException: /mnt/sdcard/data/local/textojson.txt: open failed: ENOENT (No such file or directory)
02-04 19:26:41.066: W/System.err(20167):    at libcore.io.IoBridge.open(IoBridge.java:416)
02-04 19:26:41.075: W/System.err(20167):    at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-04 19:26:41.075: W/System.err(20167):    at java.io.FileReader.<init>(FileReader.java:42)
02-04 19:26:41.075: W/System.err(20167):    at es.example.jsonarchivo.Adapter.<init>(Adapter.java:47)
02-04 19:26:41.075: W/System.err(20167):    at es.example.jsonarchivo.MainActivity.onCreate(MainActivity.java:27)
02-04 19:26:41.075: W/System.err(20167):    at android.app.Activity.performCreate(Activity.java:5008)
02-04 19:26:41.085: W/System.err(20167):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-04 19:26:41.085: W/System.err(20167):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-04 19:26:41.085: W/System.err(20167):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-04 19:26:41.085: W/System.err(20167):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-04 19:26:41.085: W/System.err(20167):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-04 19:26:41.085: W/System.err(20167):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 19:26:41.085: W/System.err(20167):    at android.os.Looper.loop(Looper.java:137)
02-04 19:26:41.085: W/System.err(20167):    at android.app.ActivityThread.main(ActivityThread.java:4745)
02-04 19:26:41.095: W/System.err(20167):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 19:26:41.105: W/System.err(20167):    at java.lang.reflect.Method.invoke(Method.java:511)
02-04 19:26:41.105: W/System.err(20167):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-04 19:26:41.105: W/System.err(20167):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 19:26:41.105: W/System.err(20167):    at dalvik.system.NativeStart.main(Native Method)
02-04 19:26:41.105: W/System.err(20167): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
02-04 19:26:41.116: W/System.err(20167):    at libcore.io.Posix.open(Native Method)
02-04 19:26:41.116: W/System.err(20167):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-04 19:26:41.116: W/System.err(20167):    at libcore.io.IoBridge.open(IoBridge.java:400)
02-04 19:26:41.125: W/System.err(20167):    ... 18 more
02-04 19:26:41.125: D/AndroidRuntime(20167): Shutting down VM
02-04 19:26:41.125: W/dalvikvm(20167): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-04 19:26:41.145: E/AndroidRuntime(20167): FATAL EXCEPTION: main
02-04 19:26:41.145: E/AndroidRuntime(20167): java.lang.RuntimeException: Unable to start activity ComponentInfo{es.example.jsonarchivo/es.example.jsonarchivo.MainActivity}: java.lang.NullPointerException
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.os.Looper.loop(Looper.java:137)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.app.ActivityThread.main(ActivityThread.java:4745)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at java.lang.reflect.Method.invoke(Method.java:511)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at dalvik.system.NativeStart.main(Native Method)
02-04 19:26:41.145: E/AndroidRuntime(20167): Caused by: java.lang.NullPointerException
02-04 19:26:41.145: E/AndroidRuntime(20167):    at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at es.example.jsonarchivo.Adapter.<init>(Adapter.java:70)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at es.example.jsonarchivo.MainActivity.onCreate(MainActivity.java:27)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.app.Activity.performCreate(Activity.java:5008)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-04 19:26:41.145: E/AndroidRuntime(20167):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-04 19:26:41.145: E/AndroidRuntime(20167):    ... 11 more
02-04 19:31:41.224: I/Process(20167): Sending signal. PID: 20167 SIG: 9

我希望我解释得很好,我的母语不是英语。

1 个答案:

答案 0 :(得分:0)

回顾一下,

  

java.io.FileNotFoundException:/mnt/sdcard/data/local/textojson.txt:open failed:ENOENT(没有这样的文件或目录)

表示该文件不存在。这可以通过f.exists()

进行验证

如果问题是权限,则应获取open failed: EACCES (Permission denied)