I am working on a project which allow user to select excel file from any folder, issue is faced when I am trying to access path by following code.
My Intent
int currentVer = android.os.Build.VERSION.SDK_INT;
if( currentVer == 29 ) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, 1);
}
else if ( currentVer == 30 ) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
| Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, 1);
}
My activity result code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if ( data != null) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
int takeFlags = data.getFlags();
takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.getContentResolver().takePersistableUriPermission(uri, takeFlags);
}
String Name = file_name(uri);
String src = uri.getPath();
File file = new File( src );
if(ExcelUtils.checkIfExcelFile(Name))
{
excelUtils.readExcel(file,FirstRowHeader); //Read Excel file content
}
}
}
} catch (Exception ex) {
}}
If I use "/sdcard/Download/Demodown.xls" this static path file is accessible, but by following line it errored out
File file = new File( src );
Error => java.io.FileNotFoundException: content:/com.android.providers.downloads.documents/document/17