2016-04-20 73 views
1

我想导入CSV在我的SQLite数据库在Android上,使用意图让用户选择CSV文件。'FileReader上没有这样的文件或目录'错误 - Android

我得到上ENOENT错误:FileReader file = new FileReader(fileName);

该文件不存在,因为我得到的路径形成的意图!

我的清单包括:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

我的意图

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType("text/csv"); 
if (intent.resolveActivity(getPackageManager()) != null) { 
    startActivityForResult(intent, REQUEST_CVS); 

我onActivityResult(...)

if (resultCode == RESULT_OK) { 
    File myPath_CSV = new File(data.getData().toString()); 
    try { 
     myDb.importCSV(myPath_CSV); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我importCSV(文件名)

public void importOreilles(File fileName) throws IOException { 
    Log.w(TAG, fileName.toString()); 
    FileReader file = new FileReader(fileName); // Getting an error !!! 
    BufferedReader buffer = new BufferedReader(file); 

    String line = null; 
    while ((line = buffer.readLine()) != null) { 
     String[] str = line.split(","); 
     insertOreille(str[0],str[1]); // Dealing with my database 
    } 
} 

我得到的错误:我使用getAbsolutePath,URI,文件试图

W/DBAdapter: file:/storage/emulated/0/Download/Oreilles.csv 
W/System.err: java.io.FileNotFoundException: /file:/storage/emulated/0/Download/ABCDE.csv: open failed: ENOENT (No such file or directory) 

...但我坚持。

任何帮助表示赞赏。

回答

1

该文件确实存在,因为我得到了一个意图的路径形式!

您决定不提供实际的“意图”以及如何使用它。我会猜测它是ACTION_GET_CONTENT。在这种情况下,您将收回Uridata.getData())。 Use ContentResolver and openInputStream()读入由Uri指向的内容。您可以将其包装在InputStreamReader中,以与现有的基于Reader的代码一起使用。

+0

刚刚添加我的意图(Intent.ACTION_GET_CONTENT的确如此) – Tibo

+0

@Tibo:你回来的'Uri'可以有'file'或'content'方案。 'ContentResolver'和'openInputStream()'将处理这两种情况,假设在'file'的情况下,[用户授予您许可](https://stackoverflow.com/questions/32635704/android-permission-犯规工作偶数如果-I-有申报-它)。 – CommonsWare

+0

从你的链接中,我认为我的问题是我对Uri是什么缺乏了解。我将看看openInputStream。 – Tibo

相关问题