打开文件不断在android文件选择器中失败 [英] opening a file keeps failing in android file chooser

查看:306
本文介绍了打开文件不断在android文件选择器中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用android文件选择器打开并读取已保存在sdCard或google驱动器或内部存储器上的文件...

成功选择文件后,当我尝试读取文件时,得到以下代码所示的文件路径,这总是使我觉得该文件不存在/没有这样的文件或目录....

我的代码:

    public class MainActivity extends AppCompatActivity {

    private static final int CHOOSE_FILE_REQUEST_CODE = 1;
    String[] ids;
    TextView textView;
    Button btnImport;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.text_view);


        btnImport = (Button) findViewById(R.id.btn_import);

        btnImport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("*/*");
                Intent i = Intent.createChooser(intent, "File");
                startActivityForResult(i, CHOOSE_FILE_REQUEST_CODE);

            }
        });



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            return;
        }
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String path = data.getData().getPath();
                    String url = data.getData().getPath();
                    File file = new File(url);
                    Log.i("***", path);
                   // InputStream inputStream = getResources().openRawResource(R.raw.sample);
                    InputStream inputStream = null ;
                    BufferedReader bufferedReader = null;

                    try {
                        inputStream = new FileInputStream(file);
                        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                        String csvLine;
                        StringBuffer stringBuffer = new StringBuffer();

                        while ((csvLine = bufferedReader.readLine()) != null) {

                            ids = csvLine.split(",");

                            for (int i = 0; i < ids.length; i++) {
                                if (i % 19 == 1) {
                                    stringBuffer.append(ids[i]);

                                    stringBuffer.append("\n");
                                    Log.i("***", stringBuffer.toString());

                                }
                                textView.setText(stringBuffer.toString());
                            }
                        }


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

                }
        }
    }
}

这是日志:

10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: java.io.FileNotFoundException: /document/E896-1C04:Download/39196035.doc: open failed: ENOENT (No such file or directory)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:76)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.example.ndrzhr.readcsv02.MainActivity$override.onActivityResult(MainActivity.java:69)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.example.ndrzhr.readcsv02.MainActivity$override.access$dispatch(MainActivity.java)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.example.ndrzhr.readcsv02.MainActivity.onActivityResult(MainActivity.java:0)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:7137)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4916)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4963)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread.access$1600(ActivityThread.java:221)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.os.Looper.loop(Looper.java:158)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7224)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at libcore.io.Posix.open(Native Method)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:  ... 15 more
 10-29 11:05:48.130 14716-14716/com.example.ndrzhr.readcsv02 I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7501f3b time:713512721
 10-29 11:15:48.520 14716-14716/com.example.ndrzhr.readcsv02 V/ActivityThread: updateVisibility : ActivityRecord{5091c6e token=android.os.BinderProxy@7501f3b {com.example.ndrzhr.readcsv02/com.example.ndrzhr.readcsv02.MainActivity}} show : true

我编辑了一些代码,但仍然得到了

10-29 12:43:27.360 19779-19779/com.example.ndrzhr.readcsv02 W/System.err: java.io.FileNotFoundException: content:/com.android.externalstorage.documents/document/primary%3ADownload%2FUntitled%20document.txt: open failed: ENOENT (No such file or directory)

为此:

    if (resultCode == RESULT_OK) {
        Uri uri = data.getData();
        String type = data.getType();
        Log.i("***", "Pick completed: " + uri + " " + type);
        if (uri != null) {
            String path = uri.toString();

            FileReader fileReader = null;
            BufferedReader bufferedReader = null;

            try {
                //inputStream = new FileInputStream(file);
                fileReader = new FileReader(path);
                bufferedReader = new BufferedReader(fileReader);
                String csvLine;
                StringBuffer stringBuffer = new StringBuffer();

                while ((csvLine = bufferedReader.readLine()) != null) {
                    ids = csvLine.split(",");
                    for (int i = 0; i < ids.length; i++) {
                        if (i % 19 == 1) {
                            stringBuffer.append(ids[i]);

                            stringBuffer.append("\n");
                            Log.i("***", stringBuffer.toString());
                            textView.setText(stringBuffer.toString());

                        }

                    }
                }


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

        }
    }
}

解决方案

以严格的方式为所选的uri内容方案打开InputStream:

InputStream is = getContentResolver().openInputStream(data.getData());

I am trying to open and read file that was been saved either on the sdCard or on the google drive or on the internal memory by using the android file chooser ...

after I successfully chosen the file and I get the file path as shown on the following code when I try to read it it keeps throwing me the that the file does not exist / no such file or directory ....

my code:

    public class MainActivity extends AppCompatActivity {

    private static final int CHOOSE_FILE_REQUEST_CODE = 1;
    String[] ids;
    TextView textView;
    Button btnImport;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.text_view);


        btnImport = (Button) findViewById(R.id.btn_import);

        btnImport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("*/*");
                Intent i = Intent.createChooser(intent, "File");
                startActivityForResult(i, CHOOSE_FILE_REQUEST_CODE);

            }
        });



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            return;
        }
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String path = data.getData().getPath();
                    String url = data.getData().getPath();
                    File file = new File(url);
                    Log.i("***", path);
                   // InputStream inputStream = getResources().openRawResource(R.raw.sample);
                    InputStream inputStream = null ;
                    BufferedReader bufferedReader = null;

                    try {
                        inputStream = new FileInputStream(file);
                        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                        String csvLine;
                        StringBuffer stringBuffer = new StringBuffer();

                        while ((csvLine = bufferedReader.readLine()) != null) {

                            ids = csvLine.split(",");

                            for (int i = 0; i < ids.length; i++) {
                                if (i % 19 == 1) {
                                    stringBuffer.append(ids[i]);

                                    stringBuffer.append("\n");
                                    Log.i("***", stringBuffer.toString());

                                }
                                textView.setText(stringBuffer.toString());
                            }
                        }


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

                }
        }
    }
}

this is the logs :

10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: java.io.FileNotFoundException: /document/E896-1C04:Download/39196035.doc: open failed: ENOENT (No such file or directory)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:76)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.example.ndrzhr.readcsv02.MainActivity$override.onActivityResult(MainActivity.java:69)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.example.ndrzhr.readcsv02.MainActivity$override.access$dispatch(MainActivity.java)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.example.ndrzhr.readcsv02.MainActivity.onActivityResult(MainActivity.java:0)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:7137)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4916)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4963)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread.access$1600(ActivityThread.java:221)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1848)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.os.Looper.loop(Looper.java:158)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7224)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at libcore.io.Posix.open(Native Method)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)
 10-29 11:05:48.080 14716-14716/com.example.ndrzhr.readcsv02 W/System.err:  ... 15 more
 10-29 11:05:48.130 14716-14716/com.example.ndrzhr.readcsv02 I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7501f3b time:713512721
 10-29 11:15:48.520 14716-14716/com.example.ndrzhr.readcsv02 V/ActivityThread: updateVisibility : ActivityRecord{5091c6e token=android.os.BinderProxy@7501f3b {com.example.ndrzhr.readcsv02/com.example.ndrzhr.readcsv02.MainActivity}} show : true

I edited some of my code but still getting this

10-29 12:43:27.360 19779-19779/com.example.ndrzhr.readcsv02 W/System.err: java.io.FileNotFoundException: content:/com.android.externalstorage.documents/document/primary%3ADownload%2FUntitled%20document.txt: open failed: ENOENT (No such file or directory)

for this :

    if (resultCode == RESULT_OK) {
        Uri uri = data.getData();
        String type = data.getType();
        Log.i("***", "Pick completed: " + uri + " " + type);
        if (uri != null) {
            String path = uri.toString();

            FileReader fileReader = null;
            BufferedReader bufferedReader = null;

            try {
                //inputStream = new FileInputStream(file);
                fileReader = new FileReader(path);
                bufferedReader = new BufferedReader(fileReader);
                String csvLine;
                StringBuffer stringBuffer = new StringBuffer();

                while ((csvLine = bufferedReader.readLine()) != null) {
                    ids = csvLine.split(",");
                    for (int i = 0; i < ids.length; i++) {
                        if (i % 19 == 1) {
                            stringBuffer.append(ids[i]);

                            stringBuffer.append("\n");
                            Log.i("***", stringBuffer.toString());
                            textView.setText(stringBuffer.toString());

                        }

                    }
                }


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

        }
    }
}

解决方案

Open an InputStream for the choosen uri content scheme in the rigth way:

InputStream is = getContentResolver().openInputStream(data.getData());

这篇关于打开文件不断在android文件选择器中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆