简体   繁体   English

如何在Android Lollipop设备的辅助SD卡中创建新文件?

[英]How to create new file in secondary SD card of Android Lollipop device?

In my app I use MediaMuxer to create a video file. 在我的应用程序中,我使用MediaMuxer创建视频文件。 Constructor of MediaMuxer requires absolute path of output file. MediaMuxer的构造函数需要输出文件的绝对路径。 Everything is ok with primary storage, but for secondary storage app doesn't work. 主存储一切正常,但辅助存储应用不起作用。 As I check, in Lollipop user has to pick output directory in external storage, using new Intent ACTION_OPEN_DOCUMENT_TREE ( How to use the new SD card access API presented for Android 5.0 (Lollipop)? ) But on result of this Intent we get DocumentFile. 正如我检查的那样,在Lollipop中,用户必须使用新的Intent ACTION_OPEN_DOCUMENT_TREE( 如何使用针对Android 5.0(Lollipop)提供的新SD卡访问API? )来选择外部存储中的输出目录。但是,由于此Intent的结果,我们得到了DocumentFile。 I don't know how to create new video file with MediaMuxer and this DocumentFile. 我不知道如何使用MediaMuxer和此DocumentFile创建新的视频文件。 Can someone help me? 有人能帮我吗?

There is a method createFile(String mimeType, String displayName) - just call it on the DocumentFile directory instance: 有一个方法createFile(String mimeType, String displayName) -只需在DocumentFile目录实例上调用它即可:

DocumentFile newFile = documentFileDir.createFile("application/octet-stream", fileName);
OutputStream os = getContentResolver().openOutputStream(newFile.getUri());
... // write your data

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

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