与Uri从SD卡获取文件 [英] Getting file from sdcard with Uri
问题描述
我让用户从SD卡上选择一个文件上传到我的服务器,并保存返回给我的 Uri
在 onActivityResult < code
示例:
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 inonActivityResult
example:
file:///storage/emulated/0/Download/menu-4.27.13.pdf
when I try to convert it to a byte array to send to the server I get the
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(); } } }
stack trace
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)
is that not what the
File
objects as a string when its initialized? Why am I getting theFileNotFoundException
when I have theUri
returned to me when the user selected the file解决方案Your
Uri
contains thefile:
scheme, which you need to remove. UsingUri.parse
, you figure the Uri contained in your String, usinguri.getPath()
, you extract the file path from the uri :Uri uri = Uri.parse(fileURI); File pdf = new File(uri.getPath());
这篇关于与Uri从SD卡获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文