本文介绍了与Uri从SD卡获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我让用户从SD卡上选择一个文件上传到我的服务器,并保存返回给我的 Uri 在 onActivityResult 示例: file:// /storage/emulated/0/Download/menu-4.27.13.pdf 当我尝试转换它到一个字节数组发送到服务器我得到 FileNotFoundException if(!fileURI.equals()){ File pdf = new File(fileURI); 尝试 { 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(); $ b $ / code $ / pre $ b $ p 11-04 11:57:30.597:W / System.err(13531):java.io.FileNotFoundException:/ file :/storage/emulated/0/Download/menu-4.27.13.pdf:打开失败:ENOENT(没有这样的文件或目录) 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):在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) :在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):在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):引起:libcore.io.ErrnoException:打开失败:ENOENT(没有这样的文件或目录) 11-04 11:57:30.647:W / System.err(13531):在libcore.io.Posix.open(本地方法) 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 ):在libcore.io.IoBridge.open(IoBridge.java:393) 是不是当初始化时, File 对象作为字符串?为什么当用户选择文件时,当 Uri 返回给我时,获取 FileNotFoundException 解决方案 您的 Uri 包含文件:方案,你需要删除。使用 Uri.parse ,您可以使用 uri.getPath()来计算字符串中包含的Uri, uri文件路径: Uri uri = Uri.parse(fileURI); 文件pdf =新文件(uri.getPath()); I let the user select a file from the sdcard to upload to my server and save the Uri that gets returned to me in onActivityResultexample:file:///storage/emulated/0/Download/menu-4.27.13.pdfwhen I try to convert it to a byte array to send to the server I get the FileNotFoundExceptionif(!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(); }}}stack trace11-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)is that not what the File objects as a string when its initialized? Why am I getting the FileNotFoundException when I have the Uri returned to me when the user selected the file 解决方案 Your Uri contains the file: scheme, which you need to remove. Using Uri.parse, you figure the Uri contained in your String, using uri.getPath(), you extract the file path from the uri :Uri uri = Uri.parse(fileURI);File pdf = new File(uri.getPath()); 这篇关于与Uri从SD卡获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 23:10