3

问题是当我在 Android Studio 中运行我的程序时,FileInputStream 找不到 c:\poi-test.xls 文件。

我在 Android Studio 中运行的简单测试 java 程序是:

try {

    File file = new File("C:\\poi-test.xls");

    FileInputStream fis = new FileInputStream(file);

} catch (FileNotFoundException e) {
    e.printStackTrace();
}

在 Android Studio 中,我单步执行并创建了文件对象,但是当我单步执行 FileInputStream 时,出现以下错误:

C:\poi-test.xls:打开失败:ENOENT(没有这样的文件或目录)

我花了几个小时试图通过 SO 和谷歌解决这个问题,但还没有找到解决方案。

完整的错误列表是:

W/System: ClassLoader referenced unknown path: /data/app/com.jake.testforreadxls-1/lib/x86_64
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
**W/System.err: java.io.FileNotFoundException: C:\poi-test.xls: open failed: ENOENT (No such file or directory)**
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:76)
W/System.err:     at com.jake.testforreadxls.MainActivity.onCreate(MainActivity.java:21)
W/System.err:     at android.app.Activity.performCreate(Activity.java:6237)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err:     at libcore.io.Posix.open(Native Method)
W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
W/System.err:   ... 14 more
Disconnected from the target VM, address: 'localhost:8734', transport: 'socket'
4

1 回答 1

0

在模拟器/安卓设备中运行的应用程序无法访问您 PC 的文件系统。Android OS 不知道在哪里C:\\poi-test.xls。文件必须存储在设备内部存储的 sd 卡上,您的解决方案才能正常工作。

于 2019-02-19T18:23:12.357 回答