I have a code in which user selects a file and program returns android.net.Uri
for the selected file. Next, the program converts Uri
to java.io.File
. When i invoke File.exists()
the program returns false
. Why the program cannot interract with existing file?
(for example, i select 1.pdf
on my microsd card, but the result will be the same if i select any other file from internal storage)
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
startActivityForResult(intent, 123);
}
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == 123
&& resultCode == Activity.RESULT_OK) {
if (resultData != null) {
Uri uri = resultData.getData();
try {
File f=new File(uri.getPath());
Log.e(TAG,"File object "+f.getAbsolutePath()); //returns /document/9016-4EF8:1.pdf
Log.e(TAG,"File exists "+f.exists()); //returns false
} catch (IOException e) {
e.printStackTrace();
}
}
}
}