FileNotFoundException although file exists in Android 13

70 Views Asked by At

hopefully someone can guide me into the right direction.

I want to open and read at least my own gpx file. Therefore, I use this code, however, I am always getting the error: FileNotFoundException although I was able to select my created gpx file!

Here is my code

public void importGPX(View view) {
    Intent data = new Intent(Intent.ACTION_GET_CONTENT);
    data.setType("*/*");
    data = Intent.createChooser(data, "Choose a file");
    startActivityResultLauncher.launch(data);
}
ActivityResultLauncher<Intent> startActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                // Handle the returned Uri
                Log.d("onActivityResult", "FileChooser works");
                File f = new File(result.toString());
                FileInputStream inputStream = null;
                try {
                    inputStream = new FileInputStream(f);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                int i;
                try {
                    i = inputStream.read();
                    while (i != -1) {
                        byteArrayOutputStream.write(i);
                        i = inputStream.read();
                    }
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String s = byteArrayOutputStream.toString();
                Log.d("FileContent", s);
            }
        });

I also added the following permission, just in case I need them.

<!-- Required only if your app needs to access images or photos
 that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<!-- Required only if your app needs to access videos
     that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

<!-- Required only if your app needs to access audio files
     that other apps created. -->
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

<!-- If your app doesn't need to access media files that other apps created,
     set the "maxSdkVersion" attribute to "28" instead. -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
    android:maxSdkVersion="32" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="29" />

Later, I would like to open other gpx files which were not created by me. But first I would be glad if I can open my own gpx file.

Thank you very much for your help in advance.

0

There are 0 best solutions below