Getting file from device on Android, keeps stating file not found when trying to use it

房东的猫 提交于 2019-12-24 11:27:04

问题


I have an Android app where I am allowing the user to pick a file from the file system, I then get the path and set the path to an EditText and then use this path to open the file contents.

Below is how I am loading the file picker

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select your private key"), PRIVATE_KEY_PICKER);

Below is my onActivityResult

        protected void onActivityResult(int requestCode, int resultCode, Intent data)
            {
                switch (requestCode)
                {
                    case PRIVATE_KEY_PICKER:
                        if (resultCode == Activity.RESULT_OK)
                        {
                            Uri uri = data.getData();

                            String path = uri.getPath();

                            txtPublicKeyPath.setText(path);
                        }
                        break;
                }
            }

    The path that I get back and set to the EditText is:

    `/document/primary:Download/my_file.txt` (my_file.txt being the file that was selected in the file picker).

    To use the file I do the following:

    File file = new File(txtPublicKeyPath.getText().toString());
                FileInputStream fis = new FileInputStream(file);

                BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }

                Intent intent = new Intent();
                intent.putExtra(PUBLIC_KEY_FILE, sb.toString());

                //If the certiticate passphrease has been provided, add this to the bundle
                if (txtPassphrase.getText().length() > 0)
                {
                    intent.putExtra(PUBLIC_KEY_PASSPHRASE, txtPassphrase.getText().toString());
                }

                setResult(Activity.RESULT_OK, intent);
                finish();
}

The above code causes the following exception:

04-10 21:46:58.680 28866-28866/com.BoardiesITSolutions.MysqlManager E/SSHKeyManager: java.io.FileNotFoundException: /document/primary:Download/id_rsa (No such file or directory)

回答1:


To use the file I do the following:

getPath() only has meaning on a Uri if the scheme is file. Your scheme is content.

Replace:

            File file = new File(txtPublicKeyPath.getText().toString());
            FileInputStream fis = new FileInputStream(file);

with:

            InputStream fis = getContentResolver().openInputStream(uri);

As a bonus, the replacement works for both file and content schemes.



来源:https://stackoverflow.com/questions/43333427/getting-file-from-device-on-android-keeps-stating-file-not-found-when-trying-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!