【问题标题】:FileNotFoundException When Uploading File (Non-image) To App将文件(非图像)上传到应用程序时出现 FileNotFoundException
【发布时间】:2017-07-10 22:10:34
【问题描述】:

我一直在尝试允许用户将文件 (PDF) 上传到我的应用程序以最终上传到我的 Parse 服务器,但是每次我尝试创建文件流/缓冲输入流时,我都会收到 FileNotFoundException,声称没有这样的文件或目录。我记录的文件路径之一如下所示:

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

我不知道为什么它给了我这条糟糕的道路。这是我的 MainActivity(我唯一的活动)。我知道并非所有文件都将存储在手机内部——这就是我为 onActivityResult 类使用某些支持方法的原因。我一直在努力隔离我的问题,因为我处理文件的时间不长,所以事情对我来说仍然相对较新。再次,我为所有代码道歉。感谢您提供任何帮助。

意图方法

public void getFile() 
{

     Intent intent = new Intent();
     intent.setType("application/pdf");
     intent.setAction(Intent.ACTION_GET_CONTENT);

     startActivityForResult(intent, 1);

 }

onActivityResult 方法

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        filename = null;
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                try {
                    Uri uri = data.getData();

                    if (1 > 1) {
                        Toast.makeText(this,"The selected file is too large. Select a new file with size less than 2mb",Toast.LENGTH_LONG).show();
                    } else {
                        String mimeType = getContentResolver().getType(uri);
                        if (mimeType == null) {
                            String path = getPath(this, uri);
                            if (path == null) {
                                filename = uri.toString().substring(uri.toString().lastIndexOf("/") + 1);
                            } else {
                                File file = new File(path);
                                filename = file.getName();
                            }
                        } else {
                            Uri returnUri = data.getData();
                            Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
                            int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                            int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
                            returnCursor.moveToFirst();
                            filename = returnCursor.getString(nameIndex);
                            String size = Long.toString(returnCursor.getLong(sizeIndex));
                        }
                        File fileSave = getExternalFilesDir(null);
                        String sourcePath = getExternalFilesDir(null) + "/" + filename;
                        sourcePath = sourcePath.substring(sourcePath.lastIndexOf("data") + 4, sourcePath.length());
                        Log.i("PATH", sourcePath);
                        Log.i("NAME", filename);

                        // create byte array
                        File file = new File(sourcePath);
                        int size = (int) file.length();
                        byte[] bytes = new byte[size];
                        try {
                            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
                            buf.read(bytes, 0, bytes.length);
                            buf.close();
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        // byte array created
                }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

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

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

【问题讨论】:

  • 它可能“看起来”准确,但显然不是。我们无法告诉您您的文件在哪里。只有你自己知道。
  • @EJP 虽然我很欣赏反对票,但我想弄清楚为什么它给了我不正确的文件路径。这不是关于特定文件,而是关于我的程序认为文件在哪里。来吧,伙计,和我一起工作。
  • 我没有说任何关于否决的事情。根据 SO 的设计和意图,您拥有其中两个,并且您对谁做的信息为零。

标签: java android file filenotfoundexception fileinputstream


【解决方案1】:

所有应用程序(无论是否为 root)都有一个默认数据目录,即 /data/data/<package_name>。默认情况下,应用程序数据库、设置和所有其他数据都放在此处。如果某个应用需要存储大量数据,或者出于其他原因想要“对内部存储友好”,则 SDCard 上有相应的目录 (Android/data/<package_name>)。

在您的代码中,在这一行

String sourcePath = getExternalFilesDir(null).toString() + "/" + filename;

filename 是实际路径。 getExternalFilesDir(null).toString()/storage/emulated/0/ 附加到sourcePath 的开头。并非所有文件都来自内部存储。所以删除它。

String sourcePath = filename;

【讨论】:

  • 不,只有文件名包含在“文件名”中。不是路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 2012-03-14
相关资源
最近更新 更多