Java 将文件(非图像)上载到应用程序时FileNotFoundException

Java 将文件(非图像)上载到应用程序时FileNotFoundException,java,android,file,filenotfoundexception,fileinputstream,Java,Android,File,Filenotfoundexception,Fileinputstream,我一直在尝试允许用户将PDF文件上载到我的应用程序,以便最终上载到我的解析服务器,但每次我尝试创建filestream/缓冲输入流时,都会收到FileNotFoundException,声称“没有这样的文件或目录”。我记录的文件路径之一显示在下面: /storage/emulated/0/Android/data/com.parse.starter/files/Eloquent_JavaScript.pdf 我不知道为什么它会给我这条不好的路。这是我的主要活动我唯一的活动。我知道并不是所有的文件

我一直在尝试允许用户将PDF文件上载到我的应用程序,以便最终上载到我的解析服务器,但每次我尝试创建filestream/缓冲输入流时,都会收到FileNotFoundException,声称“没有这样的文件或目录”。我记录的文件路径之一显示在下面:

/storage/emulated/0/Android/data/com.parse.starter/files/Eloquent_JavaScript.pdf

我不知道为什么它会给我这条不好的路。这是我的主要活动我唯一的活动。我知道并不是所有的文件都会存储在手机内部,这就是为什么我在onActivityResult类中使用了某些支持方法。我一直在努力隔离我的问题,因为我已经很长时间没有处理文件了,所以事情对我来说还是比较新的。再次,我为所有的代码道歉。感谢您的帮助

意向法

onActivityResult方法

以下是onActivityResult方法的一些支持方法


所有应用程序是否都有默认数据目录,即/data/data/。默认情况下,应用程序数据库、设置和所有其他数据都位于此处。如果一个应用程序希望存储大量数据,或者出于其他原因希望更好地使用内部存储,SDCard Android/data/上会有一个相应的目录

在代码中,在这一行

String sourcePath = getExternalFilesDir(null).toString() + "/" + filename;
filename是实际路径。getExternalFilesDirnull.toString将/storage/emulated/0/追加到sourcePath的开头。并非所有文件都来自内部存储。所以把它拿走

String sourcePath = filename;

这可能“看起来”准确,但显然不是。我们不能告诉你你的档案在哪里。只有你知道这一点。@EJP虽然我很欣赏否决票,但我正在试图弄清楚为什么它给了我错误的文件路径。这不是关于具体的文件,而是关于我的程序认为文件在哪里。来吧,伙计,和我一起工作。我没说什么反对票的事。你有两个,而且你没有关于谁做了这件事的信息,这是出于设计和意图。不,只有文件名包含在“filename”中。不是这条路。
    public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = { column };
        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }

    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is Google Photos.
     */
    public static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }
String sourcePath = getExternalFilesDir(null).toString() + "/" + filename;
String sourcePath = filename;