Hello,
Today I was looking for File Chooser for an android application Iām working on. Unfortunately most of available methods does not return a correct file path to the file.
public void btnClicked(View view)
{
Intent myFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
myFileIntent.setType("*/*");
Intent.createChooser(myFileIntent, "Choose a file");
startActivityForResult(myFileIntent, 10);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 10:
if(resultCode== RESULT_OK)
{
Uri fileUri = data.getData();
String path = FileUtils.getPath(this, fileUri);
txtFilePath.setText(path);
if(path == null || path.isEmpty())
return;
File file = new File(path);
if(file.exists())
{
Toast.makeText(this, "File Exist", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Cannot Access it!", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
I found this FileUtils.javaĀ file that really made it easy to access the the real file path of the file.
Hope this helps.
Best of luck
–Saeed š
BTC: bc1qrxmjgsc5a2fwn7zplpcu7665uq39tp2ss7luwk