繁体   English   中英

如何使用android存储访问框架附加到文件?

[英]How to append to a file using android Storage access framework?

如何使用存储访问框架将 FileOutputStream 附加到文件? 以下代码覆盖文件

void write(Uri uri){
        try {
            ParcelFileDescriptor descriptor=getContentResolver().openFileDescriptor(uri,"w");
            if(descriptor!=null) {
                FileOutputStream op=new FileOutputStream(descriptor.getFileDescriptor());
                op.write("test string".getBytes());
                op.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我知道使用FileOutputStream(File file,boolean append)用 java.io.File 来做

但我看不到FileOutputStream(FileDescriptor fd,boolean append)

如果要附加到文件,则需要在此调用中使用访问模式“wa”而不是“w”:

ParcelFileDescriptor descriptor =
    getContentResolver().openFileDescriptor(uri, "w");

而不是从创建 FileOutputStream

FileOutputStream op = new FileOutputStream(descriptor.getFileDescriptor());

你可以直接使用

context.getContentResolver().openOutputStream(yourUri, "wa");

并提供该下列模式w wa rwrwt并追加现有的文件使用wa

暂无
暂无

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

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