【问题标题】:FileNotFoundException when accessing file stored at /storage/emulated/0 directory in android访问存储在 android 中 /storage/emulated/0 目录中的文件时出现 FileNotFoundException
【发布时间】:2013-07-17 23:55:37
【问题描述】:

我正在尝试通过 POST HTTP 方法将 android 中 /storage/emulated/0/Videos/someVideo.3gp 中保存的录制视频文件传递到远程服务器。我使用this 作为将视频文件以 FileBody 格式传递到远程服务器的一种方式。但是,最后当我尝试执行httpClient.execute(request) 命令时,它只会继续抛出FileNotFoundException

所以,我不明白为什么外部世界无法访问视频文件,因为我已使用 getExternalStorageDirectory() 将其保存到 sdcard。我还为清单文件添加了写权限。

附:我正在使用 Nexus 7 进行测试。请帮忙..

提前致谢!

【问题讨论】:

    标签: android http-post sd-card filenotfoundexception


    【解决方案1】:

    你在执行httpClient.execute(request)之前检查文件是否存在?

    引用自您所关注的问题:

    File sourceFile = new File(sourceFileUri);
    if (!sourceFile.isFile()) {
    Log.e("Huzza", "Source File Does not exist");
    return 0;
    }
    

    编辑:
    可能是您没有在onActivityResult(...) method 上正确转换 Uri 的路径 我正在分享获取正确路径的代码,它在我的应用程序中运行良好。

    String videoPath = "";
    try 
    { 
     String[] filePathColumn = { MediaStore.Video.Media.DATA };
     Cursor cursor = getContentResolver().query(selectedVideoUri,
     filePathColumn, null, null, null);
     cursor.moveToFirst();
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
     videoPath = cursor.getString(columnIndex);
     cursor.close();
    } catch (Exception e) {
     Log.e(TAG,
     "Error parsing Video path = " + e.toString());
    }
    

    【讨论】:

    • 我现在添加了支票..而且,该文件不存在。但是,我不知道错误的原因。因为我在视频录制完成后得到了意图返回的文件路径..
    • 感谢您指出我实施中的问题。早些时候,我实际上试图将录制的视频保存在 sdcard 的自定义文件夹中,因此它不起作用。但是,当我尝试您的代码并将其保存到其默认位置时,它起作用了..!谢谢..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 2021-10-14
    • 2016-11-07
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多