1

I'm trying to import a CSV in my SQLite database on Android, using an intent to have the user choose the CSV file.

I get an ENOENT error on : FileReader file = new FileReader(fileName);

The file does exist since I get the path form an intent !

My manifest includes :

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

My intent :

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

My onActivityResult(...) :

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

My importCSV(fileName) :

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
    }
}

The error I'm getting :

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)

I tried using getAbsolutePath, Uri, File... but I'm stuck.

Any help appreciated.

Tibo
  • 383
  • 5
  • 27

1 Answers1

1

The file does exist since I get the path form an intent !

You decided not to provide the actual "intent" and how you use it. I am going to guess that it is ACTION_GET_CONTENT. In that case, you get back a Uri (data.getData()). Use ContentResolver and openInputStream() to read in the contents pointed to by that Uri. You can wrap that in an InputStreamReader to use with your existing Reader-based code.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Just added My intent (Intent.ACTION_GET_CONTENT indeed) – Tibo Apr 20 '16 at 17:28
  • @Tibo: The `Uri` that you get back could have either a `file` or a `content` scheme. `ContentResolver` and `openInputStream()` will handle both cases, assuming that, in the case of a `file`, [the user granted you permission](https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it). – CommonsWare Apr 20 '16 at 17:30
  • From your link, I think my problem is my lack of understanding of what is a Uri. I'm gonna have a look on openInputStream. – Tibo Apr 20 '16 at 17:37
  • I'm still lost. Suppose i use `InputStream is = getContentResolver().openInputStream(Uri_CSV)`. How am I supposed to use the `is` to read the lines of the CSV file ? – Tibo Apr 20 '16 at 18:05
  • @Tibo: As I wrote in my answer, you can wrap that in an `InputStreamReader` for use with your existing CSV code. Or, use a CSV library. – CommonsWare Apr 20 '16 at 18:46
  • Thanks. I got it working with `InputStreamReader isr = new InputStreamReader(inStream);` and `BufferedReader buffer = new BufferedReader(isr);` – Tibo Apr 20 '16 at 19:08