FileNotFoundException从android内部存储FileInputStream读取文件

时间:2015-07-20 12:11:23

标签: android readfile filenotfoundexception fileinputstream

您好,我无法使用FileInputStream读取文件内容。我使用的代码如下:

    List<ContentValues> poisList = new ArrayList<ContentValues>();
    try {
        FileInputStream fis = new FileInputStream (new File(filePath));
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String line = "";

        while ((line = br.readLine()) != null) {
            viewHolder.logout.setText(line);
            readPOI(poisList, line, categoryID);
        }
        br.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(getActivity(), "File", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(getActivity(), "IO", Toast.LENGTH_LONG).show();
    }

变量 filePath 的值为:&#34; / document / primary:Download / Taiwan.txt&#34;。我通过使用Intent.ACTION_GET_CONTENT和使用后得到它:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 1){
        if(resultCode == Activity.RESULT_OK){
            filePath = data.getData().getPath();

我还使用了以下两个选项来获取文件,但也没有工作:

        FileInputStream fis = getActivity().openFileInput(filePath);
          InputStream is = getClass().getResourceAsStream(filePath);

错误是这样的:

07-20 13:33:20.247    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ java.io.FileNotFoundException: /document/primary:Download/Taiwan.txt: open failed: ENOENT (No such file or directory)
07-20 13:33:20.247    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:457)
07-20 13:33:20.257    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-20 13:33:20.257    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at com.example.rafa.liquidgalaxypoiscontroller.AdminFragment.readFile(AdminFragment.java:74)
07-20 13:33:20.267    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at com.example.rafa.liquidgalaxypoiscontroller.AdminFragment.onActivityResult(AdminFragment.java:176)
07-20 13:33:20.267    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
07-20 13:33:20.277    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at android.app.Activity.dispatchActivityResult(Activity.java:5467)
07-20 13:33:20.277    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at android.app.ActivityThread.deliverResults(ActivityThread.java:3383)
07-20 13:33:20.287    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at android.app.ActivityThread.handleSendResult(ActivityThread.java:3430)
07-20 13:33:20.287    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at android.app.ActivityThread.access$1300(ActivityThread.java:142)
07-20 13:33:20.297    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1251)
07-20 13:33:20.297    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
07-20 13:33:20.307    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
07-20 13:33:20.307    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5118)
07-20 13:33:20.317    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-20 13:33:20.317    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
07-20 13:33:20.317    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-20 13:33:20.327    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
07-20 13:33:20.327    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
07-20 13:33:20.327    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-20 13:33:20.337    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at libcore.io.Posix.open(Native Method)
07-20 13:33:20.337    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-20 13:33:20.337    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:441)
07-20 13:33:20.337    6089-6089/com.example.rafa.liquidgalaxypoiscontroller W/System.err﹕ ... 17 more

我能做什么,请??? 谢谢你

0 个答案:

没有答案