unable to find suitable app to open the file in android 10 and up. I have tested it on android 9 and it worked fine

24 Views Asked by At
private void openFile(String fileLocation) {
        File file = new File(fileLocation);

        if (!file.exists()) {
            Log.e(TAG, "File does not exist: " + fileLocation);
            return;
        }

        MediaScannerConnection.scanFile(this, new String[]{file.getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String path, Uri uri) {
                Log.i(TAG, "File scanned: " + uri.toString());

                // Get the file extension
                String extension = MimeTypeMap.getFileExtensionFromUrl(fileLocation);
                // Use the file extension to get the MIME type
                String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(uri, mimeType); // Set the specific MIME type
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);
                } else {
                    intent.setDataAndType(uri,"*/*");
                    startActivity(intent);
                    Log.e(TAG, "No suitable app to open the file: " + uri.toString());
                }
            }
        });
    }

I have passed /storage/emulated/0/Download/Untitled-1.png this location in the function and it converted the location to uri content://media/external_primary/images/media/1000002470.it is not detecting the correct app for the file

0

There are 0 best solutions below