简体   繁体   English

Android,NDK故意将其写入SD卡文件

[英]Android , ndk, write willfully to a sd card file

After Android 4.4, the SD card needs permission to write files. 在Android 4.4之后,SD卡需要具有写入文件的权限。 You can request to write to the SD card by StorageVolume. 您可以通过StorageVolume请求写入SD卡。 createAccessIntent (). createAccessIntent()。 The stream is then accessed using ContentResolver().openoutputstream (file.geturi ()) to write data to the file. 然后使用ContentResolver()。openoutputstream(file.geturi())访问流,以将数据写入文件。 How do I write data in the NDK? 如何在NDK中写入数据?

public void startRequestPermissions(Context context) {
        Intent intent = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            if (mStorageManager != null) {
                StorageVolume volume = mStorageManager.getStorageVolume(new File(timePath));
                if (volume != null) {
                    intent = volume.createAccessIntent(null);
                }
            }
        }
        if (intent == null) {
            intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        }
        ((Activity) context).startActivityForResult(intent, FileBrowserConfig.OPEN_DOCUMENT_TREE_CODE);
    }



 public OutputStream getOutputStream(Context context, File destFile) {
        OutputStream out = null;
        try {
            DocumentFile file = getDocumentFile(destFile, false, context);
            out = context.getContentResolver().openOutputStream(file.getUri());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return out;
    }

Can write data in sd card with NDK! 可以使用NDK将数据写入SD卡!

除了openOutputStream()之外,您还可以使用openAssetFileDescriptor()获得文件描述符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM