With the new way Android 11 Scoped Storage works, it stressing me out to learn it. I'm stil using Java and the lack of Java tutorial for Android 11 is insane!
I'm making an app that would open a .csv
file to read it contents and add them to a arrayList
so I can then show it on a textview
, the thing is, even tho I'm reading the Android docs and all, nothing works! Here is what I did so far:
Following the tutorial on developer.android: https://developer.android.com/training/data-storage/shared/documents-files
private void openFile() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, true);
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
}
This way I can open the filepicker
and navigate to my files, then, to read the .csv
I'm using ReaderCSV
:
if (requestCode == PICKFILE_REQUEST_CODE
&& resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (data != null) {
uri = data.getData();
try {
CSVReader reader = new CSVReader(new FileReader(uri.toString()));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(nextLine[0] nextLine[1] "etc...");
}
} catch (IOException | CsvValidationException e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
//Toast.makeText(this, uri.toString(), Toast.LENGTH_SHORT).show();
}
}
But then when I try to use the uri result it gives me a error of:
"java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/document:4165: open failed: ENOENT (No such file or directory)"
Now I'm stuck and bugged out because I can't solve it in anyway, shape or form.
On my Manifest I have the permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
Edit: This is how I solved my particular problem.
if (requestCode == 1
&& resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (data != null) {
uri = data.getData();
try {
myList.clear();
InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
String mLine;
while ((mLine = r.readLine()) != null) {
myList.add(mLine);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
reload_myList();
}
}
This is working, but there is a catch, since my .CSV file is just one column of data, it does work because of readLine(), if you are using a completly different .CSV file with many data separated on (,) do not use this method like how I did, cuz I think it will not behave like you want, I'm pretty sure you should add somekinda of regex(",") while reading the file, but I don't know how and I'm so happy it worked out!
Basically what I had to do to get the contents of the file was to use:
InputStream inputStream = getContentResolver().openInputStream(uri);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
CodePudding user response:
I think the issue is in this line:
CSVReader reader = new CSVReader(new FileReader(uri.toString()));
When you work with Uri, you don't actually hold the real path of the file. So you cannot create File object from uri.toString().
My suggestion:
- Try a different constructor of CSVReader. You can open an InputStream from Uri from Android and read that stream.