1

I am trying to read the contents of a .csv file which is in my downloads folder of internal storage of my phone. My code looks like this.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    browse = (Button) findViewById(R.id.browse);
    editTextPath = (EditText) findViewById(R.id.path);
    browse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(Intent.createChooser(intent,"Select file or dir"), 1);
            setResult(Activity.RESULT_OK);
            Log.e("Content","In onclick");
        }
    });}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.e("Content","In onactivity");
    if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
        String path = data.getData().getPath();
        Log.e("Pathb",path);
        proImportCSV(new File(data.getData().getPath()));
    }

}

private void proImportCSV(File file) {
    try{
        ContentValues cv = new ContentValues();
        FileReader fr = new FileReader(file);
        CSVReader dataRead = new CSVReader(fr);
        String[] vv = null;
        while((vv = dataRead.readNext())!=null) {
            cv.clear();
        }
        dataRead.close();
    }
    catch (Exception e) { Log.e("TAG",e.toString());

    }
}

The error I am getting is:

java.io.FileNotFoundException: /document/primary:Download/12132469_About_0_22092018045225.csv: open failed: ENOENT (No such file or directory)

I am also sharing a screenshot of my error:

enter image description here

Update

I made a small change to my previously shared code:

private void proImportCSV(File file, Uri uri) {
    try{
        ContentValues cv = new ContentValues();
        InputStream inputStream = getContentResolver().openInputStream(uri);
        InputStreamReader isr = new InputStreamReader(inputStream);
        CSVReader dataRead = new CSVReader(isr);
        String[] vv = null;
        while((vv = dataRead.readNext())!=null) {
            cv.clear();
        }
        dataRead.close();
    }
    catch (Exception e) { Log.e("TAG",e.toString());

    }
}

Here, uri = data.getData(). After this change I am getting the following error

java.io.FileNotFoundException: /storage/emulated/0/Download/12132469_About_0_22092018045225.csv: open failed: EACCES (Permission denied)

Also, I have already added this lines of code to my manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
halfer
  • 19,824
  • 17
  • 99
  • 186
Vihar Bhatt
  • 19
  • 1
  • 3
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 04 '18 at 08:21
  • Thanks for the suggestion. I am new to asking questions on stack overflow so had no idea about that. Will be careful the next time I post. – Vihar Bhatt Dec 05 '18 at 11:04

1 Answers1

2

You not always can create a working file from a content URI. But you likely don't need it. new CSVReader(fr); accepts a Reader as a parameter. You can use InputStreamReader, created from the InputStream, created from the Uri using following method:

InputStream  inputStream = getContentResolver().openInputStream(uri);

where uri is data.getData()

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52