我讓用戶從SD卡中選擇要上傳的文件到我的服務器,並保存獲取onActivityResult
充分利用SD卡文件,烏里
例如還給我Uri
:
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)
是不是對象作爲初始化時的字符串?爲什麼我收到FileNotFoundException
當我有Uri
返回給我當用戶選擇文件
可能是你可以使用內容解析器 見 - http://stackoverflow.com/questions/10021528/open-file-from-uri-independent-of-location-in-android – ranjk89
日誌中的文件路徑「/file:/storage/emulated/0/Download/menu-4.27.13.pdf」不正確。也許你正在使用不正確的方式來獲取文件路徑。 – jagmohan