This is my code:
In the code below, I am choosing a file and getting its data and storing it as uri:
Uri uri = null;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent data = new Intent(Intent.ACTION_OPEN_DOCUMENT);
data.setType("*/*");
data = Intent.createChooser(data, "Choose a file");
launch_activity.launch(data);
}
});
ActivityResultLauncher<Intent> launch_activity = registerForActivityResult
(new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
try {
Intent data = result.getData();
if (data != null) {
uri = data.getData();
}
}
}
});
Here I am fetching the file object from my uri
File file;
try {
file = new File(uri.getPath());
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
fileInputStream = new FileInputStream(file); //error occurs here
} catch (FileNotFoundException e) {
e.printStackTrace();
}
I keep getting this error when I execute the code:
java.io.FileNotFoundException: /document/document:277030: open failed: ENOENT (No such file or directory)
What does it mean? How do I fix it?
EDIT: I have even added this line in my manifest:
android:requestLegacyExternalStorage="true"
Also I have added this permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
But I still get FileNotFoundException