0

我正在创建一个应用程序,需要能够在特定目录中创建新文件。但应用程序无法在 Android上SD 卡/可移动磁盘上存储的另一个目录中创建新文件。我写了以下函数:

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;
}

以及处理文件创建过程的以下代码,

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

但是这样的日志会发生错误:

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

我在设备上的目录“sdcard0”中创建文件没有问题,但是在目录“scard1”/可移动存储中创建新文件时总是失败(如果我的卡 ID 是 4454-1BFB。也许......)。

我有像这样用KOTLIN编写的请求权限代码。

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
        )
    }
}

如何在可移动存储上创建新文件?我是否也必须在功能请求权限中添加代码?,非常感谢您的回答。

4

0 回答 0