Error to write new file to other application data directory in removable storage using Android SDK 21

164 Views Asked by At

I'm creat an application and need to be able to create new files in a specific directory. But the application fails to create a new file in another directory stored on the SD-Card / Removable Disk on Android. I wrote the following function:

private static String getExternalStoragePath(Context mContext) {
    StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
    Class<?> storageVolumeClazz = null;
    try {
        storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
        Object result = getVolumeList.invoke(mStorageManager);
        final int length = Array.getLength(result);
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            String path = (String) getPath.invoke(storageVolumeElement);
            boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
            if (removable == true) {
                return path;
            }
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}

and the following code to handle the file creation process,

...
String dirOtherApp = "/Android/data/com.other.application/";
String sdCardPath = getExternalStoragePath(context);
String fileName = "new_file.txt";
final File removableStorage = new File(sdCardPath + dirOtherApp + "/" + fileName);
...

but an error occurs with a log like this:

Caused by: java.io.FileNotFoundException: /storage/4454-1BFB/Android/data/Android/data/com.other.application/new_file.txt (Permission denied)

I have no problem creating files in the directory "sdcard0" on the device, but it always fails when creating new files in the directory "scard1" / removable storage ( in case my card Id is 4454-1BFB. maybe... ).

I have the code for request permssion written in KOTLIN like this.

private val readStoragePermission: Int = 11
@RequiresApi(Build.VERSION_CODES.M)
private fun requestPhonePermissions() {
    if (ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
            readStoragePermission
        )
    }
    if (ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.READ_EXTERNAL_STORAGE
        )
        != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
            readStoragePermission
        )
    }
    if (ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.READ_EXTERNAL_STORAGE
        )
        != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
            readStoragePermission
        )
    }
    if (ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.ACCESS_NOTIFICATION_POLICY
        )
        != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.ACCESS_NOTIFICATION_POLICY),
            readStoragePermission
        )
    }
}

How can I create a new file on the removable storage? do I have to add code to the function request permission too ?, Thank you very much for your answer.

0

There are 0 best solutions below