簡體   English   中英

無法從 Android 應用程序的下載文件夾中讀取文件

[英]Unable to read file from the Downloads folder for an android app

我想為存儲在手機下載文件夾中的文件創建輸入流。 早些時候,我將所需的文件作為資產,並使用以下代碼閱讀了它:

InputStream fin;
fin = getAssets().open(FILENAME);

但是現在我希望能夠將存儲在我的下載文件夾/mnt/sdcard/Download作為 InputStream 而不是FileInputStream 對象打開。 我正在使用一個只需要 InputStream 作為參數的庫,所以我不能使用 FileInputStream 方法,因為如果我這樣做並且應用程序停止,我會得到一個fatal exception

我該怎么做? 我試圖從文檔中找到一些幫助函數,但找不到。

編輯:我也嘗試將代碼編輯為此-

String path = "/mnt/sdcard/Download"+FILENAME;
fin = new BufferedInputStream(new FileInputStream(path));

但是,當我嘗試讀取文件時,應用程序崩潰了。

我和你有同樣的問題:

AssetManager am=getAssets();
InputStream is=am.open("file1.xls");
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);

在這里,我曾經從資產文件夾訪問 file1.xls....

我使用以下方法修復了它:

File filepath = newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+"file1.xls");  // fist part gets the directory of download folder and last part is your file name
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);

不要忘記先獲得存儲權限才能訪問存儲....

希望這有效....🙂🙂

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM