Android 正在使用Uri从SD卡获取文件
我让用户从SD卡中选择一个文件上传到我的服务器,并保存Android 正在使用Uri从SD卡获取文件,android,file,filenotfoundexception,Android,File,Filenotfoundexception,我让用户从SD卡中选择一个文件上传到我的服务器,并保存Uri,该Uri在onActivityResult 例如: file:///storage/emulated/0/Download/menu-4.27.13.pdf 当我试图将其转换为字节数组以发送到服务器时,我得到了FileNotFoundException if(!fileURI.equals("")){ File pdf = new File(fileURI); try { FileInput
Uri
,该Uri在onActivityResult
例如:
file:///storage/emulated/0/Download/menu-4.27.13.pdf
当我试图将其转换为字节数组以发送到服务器时,我得到了FileNotFoundException
if(!fileURI.equals("")){
File pdf = new File(fileURI);
try
{
FileInputStream fin = new FileInputStream(pdf);
byte fileContent[] = new byte[(int)pdf.length()];
fin.read(fileContent);
fin.close();
String pdfString = Base64.encode(fileContent);
sb.append(pdfString);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
堆栈跟踪
11-04 11:57:30.597: W/System.err(13531): java.io.FileNotFoundException: /file:/storage/emulated/0/Download/menu-4.27.13.pdf: open failed: ENOENT (No such file or directory)
11-04 11:57:30.597: W/System.err(13531): at libcore.io.IoBridge.open(IoBridge.java:409)
11-04 11:57:30.607: W/System.err(13531): at java.io.FileInputStream.<init>(FileInputStream.java:78)
11-04 11:57:30.607: W/System.err(13531): at com.ecm2.mobilemap.services.MessageService.getModifiedElements(MessageService.java:2755)
11-04 11:57:30.617: W/System.err(13531): at com.ecm2.mobilemap.services.MessageService.callSync(MessageService.java:2433)
11-04 11:57:30.617: W/System.err(13531): at com.ecm2.mobilemap.services.MessageService.onHandleIntent(MessageService.java:190)
11-04 11:57:30.627: W/System.err(13531): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-04 11:57:30.627: W/System.err(13531): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 11:57:30.637: W/System.err(13531): at android.os.Looper.loop(Looper.java:137)
11-04 11:57:30.637: W/System.err(13531): at android.os.HandlerThread.run(HandlerThread.java:61)
11-04 11:57:30.637: W/System.err(13531): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
11-04 11:57:30.647: W/System.err(13531): at libcore.io.Posix.open(Native Method)
11-04 11:57:30.657: W/System.err(13531): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-04 11:57:30.657: W/System.err(13531): at libcore.io.IoBridge.open(IoBridge.java:393)
11-04 11:57:30.597:W/System.err(13531):java.io.FileNotFoundException:/file:/storage/simulated/0/Download/menu-4.27.13.pdf:open失败:enoint(没有这样的文件或目录)
11-04 11:57:30.597:W/System.err(13531):位于libcore.io.IoBridge.open(IoBridge.java:409)
11-04 11:57:30.607:W/System.err(13531):位于java.io.FileInputStream。(FileInputStream.java:78)
11-04 11:57:30.607:W/System.err(13531):位于com.ecm2.mobilemap.services.MessageService.getModifiedElements(MessageService.java:2755)
11-04 11:57:30.617:W/System.err(13531):位于com.ecm2.mobilemap.services.MessageService.callSync(MessageService.java:2433)
11-04 11:57:30.617:W/System.err(13531):位于com.ecm2.mobilemap.services.MessageService.onhandleint(MessageService.java:190)
11-04 11:57:30.627:W/System.err(13531):位于android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
11-04 11:57:30.627:W/System.err(13531):位于android.os.Handler.dispatchMessage(Handler.java:99)
11-04 11:57:30.637:W/System.err(13531):位于android.os.Looper.loop(Looper.java:137)
11-04 11:57:30.637:W/System.err(13531):在android.os.HandlerThread.run(HandlerThread.java:61)
11-04 11:57:30.637:W/System.err(13531):原因:libcore.io.ErrnoException:open失败:enoint(没有这样的文件或目录)
11-04 11:57:30.647:W/System.err(13531):位于libcore.io.Posix.open(本机方法)
11-04 11:57:30.657:W/System.err(13531):位于libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-04 11:57:30.657:W/System.err(13531):位于libcore.io.IoBridge.open(IoBridge.java:393)
这不是
文件
初始化时作为字符串对象的内容吗?当用户选择文件时,Uri
返回给我时,为什么我会得到FileNotFoundException
,您的代码是正确的,可能是文件不存在或路径错误。
查看堆栈跟踪。第一行本身说明了整个问题。尝试访问其他文件并首先使用URI解析文件路径。您的
URI
包含文件:
方案,需要删除该方案。使用Uri.parse
,计算字符串中包含的Uri;使用Uri.getPath()
,从Uri中提取文件路径:
Uri uri = Uri.parse(fileURI);
File pdf = new File(uri.getPath());
可能是您可以使用内容解析器查看日志中的-File path“/File:/storage/emulated/0/Download/menu-4.27.13.pdf”不正确。可能是您使用了不正确的方法来获取文件路径。这花了我很多时间才找到问题的答案,但仔细想想,这一点是显而易见的。谢谢你在这种情况下fileURI是什么?@KalaJ:cf问题:示例:
file:///storage/emulated/0/Download/menu-4.27.13.pdf
过了一会儿就明白了。非常感谢。你救了我一天!