androidstorageandroid-permissionsandroid-external-storagefile-management

I want to access a map file which is in my internal storage in android 13 and above


I am developing a map application that have a map file not in its assets or raw but on the users phone for instance country.map so how do i access by giving right permission Media.IMAGES or Media.AUDIO OR Media.VIDEO doesnt represent my file. Please give me some solutions. Thanks in advance!!!

I tried to add all these permissions but nothing is coming as a files and media permission in my android smartphone version 13 these are the permissions :

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" tools:ignore="ProtectedPermissions" /><uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" tools:ignore="ProtectedPermissions" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_MEDIA_AUDIO" /><uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /><uses-permission android:name="android.permission.READ_MEDIA_VIDEO" /><uses-permission android:name="android.permission.READ_MEDIA_FILES" tools:ignore="SystemPermissionTypo" /><uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" tools:ignore="ProtectedPermissions" /><uses-permission android:name="android.permission.MANAGE_MEDIA" tools:ignore="ProtectedPermissions" /><uses-permission android:name="android.permission.CALL_PHONE" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.READ_PHONE_STATE" />


Solution

  • Finally after doing a lot research I found the solution, that for android versions below 12 we need to add the storage permissions, but as for 13 and above the permission is not needed if YOU ARE ACCESSING YOUR OWN FILES AND GENERATED BY YOURSELF. Just save and access your files, for my map file this is my code for extracting to external storage,

      private void unzip(File zipFile, File destination) throws IOException {
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry;
        byte[] buffer = new byte[1024];
        int count;
    
        while ((entry = zis.getNextEntry()) != null) {
            File file;
            if (entry.isDirectory()) {
                file = new File(destination, entry.getName());
                file.mkdirs();
            } else {
                String entryName = entry.getName();
                String folderName = "daero" + File.separator + "maps";
                String relativePath = entryName.substring(entryName.indexOf('/') + 1); // Removing the first directory from the path
    
                file = new File(destination, folderName + File.separator + relativePath);
    
                File parentDir = file.getParentFile();
                if (parentDir != null && !parentDir.exists()) {
                    parentDir.mkdirs();
                }
    
                if (file.exists()) {
                    file.delete();
                }
    
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
    
                while ((count = zis.read(buffer)) != -1) {
                    bos.write(buffer, 0, count);
                }
    
                bos.flush();
                bos.close();
            }
    
            zis.closeEntry();
        }
    
        zis.close();
    
    }