【问题标题】:Getting file from device on Android, keeps stating file not found when trying to use it从Android上的设备获取文件,尝试使用时一直提示找不到文件
【发布时间】:2017-04-10 21:49:15
【问题描述】:

我有一个 Android 应用程序,允许用户从文件系统中选择文件,然后获取路径并将路径设置为 EditText,然后使用此路径打开文件内容。

下面是我如何加载文件选择器

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);

下面是我的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();
}

以上代码导致如下异常:

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)

【问题讨论】:

    标签: android file filepicker


    【解决方案1】:

    要使用该文件,我执行以下操作:

    getPath() 仅在方案为file 时对Uri 有意义。你的方案是content

    替换:

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

    与:

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

    作为奖励,替换适用于 filecontent 方案。

    【讨论】:

    • 谢谢,从未见过这样做的方式,总是看到正在使用 FileInputStream 和 File。感谢您的帮助
    猜你喜欢
    • 2011-12-27
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    相关资源
    最近更新 更多