[英]Android: Trouble opening a user selected CSV or EXCEL file
我正在創建一個應用程序,我需要用戶能夠將數據導入設備上的本地Room數據庫中。 我正在利用startActivityForResult(...)
方法從Intent.ACTION_GET_CONTENT
顯示的Intent.ACTION_GET_CONTENT
獲得響應:
Intent intent = new Intent().setType("*/*").setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"), GET_FILE_INTENT_CODE);
在要求用戶指定要導入的文件的布局並將其轉換為ImportStrategy
我將strategy
和onActivityResult(...)
收到的Uri
傳遞給我的importData
方法
void importData(Uri data, ImportStrategy strategy) {
try {
File csvFile = new File(Environment.getExternalStorageDirectory() + data.getPath());
CSVReader reader = new CSVReader(new FileReader(csvFile.getAbsolutePath()));
String[] nextLine;
List<RainfallRecord> records = new ArrayList<>();
while ((nextLine = reader.readNext()) != null) {
records.add(getRecord(nextLine, strategy));
}
db.recordDao().insertAll(records);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this.getApplicationContext(), "Error loading file", Toast.LENGTH_LONG).show();
} catch (IOException ex) {
ex.printStackTrace();
Toast.makeText(this.getApplicationContext(), "Error processing file", Toast.LENGTH_LONG).show();
}
}
我遇到的問題是當它到達創建CSVReader的行時,我得到了FileNotFoundException
堆棧跟蹤:
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/document/1945 (No such file or directory)
W/System.err: at java.io.FileInputStream.open0(Native Method)
W/System.err: at java.io.FileInputStream.open(FileInputStream.java:231)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:165)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:112)
W/System.err: at java.io.FileReader.<init>(FileReader.java:58)
W/System.err: at ekshore.net.raintracker.MainActivity.importData(MainActivity.java:351)
W/System.err: at ekshore.net.raintracker.MainActivity.lambda$customStrategy$11(MainActivity.java:406)
W/System.err: at ekshore.net.raintracker.-$$Lambda$MainActivity$A7AYI1UOo4MNVQeFvopgj2wHPMM.onClick(Unknown Source:11)
W/System.err: at android.view.View.performClick(View.java:6597)
W/System.err: at android.view.View.performClickInternal(View.java:6574)
W/System.err: at android.view.View.access$3100(View.java:778)
W/System.err: at android.view.View$PerformClick.run(View.java:25885)
W/System.err: at android.os.Handler.handleCallback(Handler.java:873)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err: at android.os.Looper.loop(Looper.java:193)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6669)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
返回的URI是content://com.android.providers.downloads.documents/document/1945
最后的1945
應該是Rain(2018).xlsx
在我對此問題所做的所有研究中,我都發現從URI類型的content
加載文件存在一些問題。 但是我看到的所有修復都是針對媒體文件(圖像或音頻)的,而不是像這樣的面向數據的文件。 當我嘗試模仿使用ContentResolver
其他解決方案時,我無法弄清楚。
如果有人可以幫我解決這個問題,我將不勝感激。
經過更多研究后,我偶然發現了該博客 ,發現我一直在嘗試以錯誤的方式進行操作。 我需要打開文件的方式不是通過File
對象, File
對象需要一個文件路徑。 相反,我需要使用ContentResolver
創建和InputStream
並以這種方式獲取數據,代碼最終看起來像這樣:
try {
ContentResolver contentResolver = this.getContentResolver();
InputStream inputStream = contentResolver.openInputStream(data);
Scanner scanner = new Scanner(inputStream).useDelimiter("\n");
String nextLine = scanner.nextLine();
List<RainfallRecord> records = new ArrayList<>();
if (nextLine.matches("^[A-Za-z]"));
nextLine = scanner.nextLine();
while (scanner.hasNext()) {
String[] rainData = nextLine.split(",");
nextLine = scanner.nextLine();
records.add(getRecord(rainData, strategy));
}
db.recordDao().insertAll(records);
} catch (IOException ex) {
ex.printStackTrace();
Toast.makeText(this.getApplicationContext(), "Error processing file", Toast.LENGTH_LONG).show();
}
if (nextLine.matches("^[A-Za-z]"));
正在仔細檢查是否有標題信息,如果有則跳過它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.