SAF - ACTION_CREATE_DOCUMENT - 将文件保存到Drive后文件为空

时间:2017-12-20 15:03:21

标签: android storage-access-framework

我是SAF的初学者。我想要做的是保存配置非常简单。假设该文件是.conf。

我将.conf复制到conf.txt并将其保存在云端硬盘上。

这是我的代码:

            tools.deleteFile(dst);   // delete conf.txt if it exists
            int res = tools.copyFile(src,dst); // copy .conf to conf.txt
            if(res == -1) return;

            tools.viewFile(dst);  
// verify in Log info that the content of cnf.txt is correct

            Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TITLE, dst);
            startActivity(intent);

我在云端硬盘中保存。该文件出现在我的电脑上,但当我打开它时,它是空的。

当我做反向时:ACTION_OPEN_DOCUMENT

   public void onActivityResult(int requestCode, int resultCode,
                                 Intent resultData) {
    Uri uri;

    if (resultCode == Activity.RESULT_OK){
         if (requestCode == 30){
            if (resultData != null) {
                uri = resultData.getData();
                try {
                    String content =
                            readFile(uri);
                } catch (IOException e) {
                    e.printStackTrace();
                }

函数readFile打开文件并在读取时停止,因为没有数据。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

Intent(Intent.ACTION_CREATE_DOCUMENT)用于创建文本文件,并使用onActivityResult()从文件中获取uri(位置),何时您使用OutputStream 写入数据(字节[])到文件。


private void createAndSaveFile() {
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TITLE, "testFileSam.txt");

    startActivityForResult(intent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            try {
                Uri uri = data.getData();

                OutputStream outputStream = getContentResolver().openOutputStream(uri);

                outputStream.write("Hi, welcome to Sam's Android classroom! Have a good day!".getBytes());

                outputStream.close();

                Toast.makeText(this, "Write file successfully", Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                Toast.makeText(this, "Fail to write file", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "File not saved", Toast.LENGTH_SHORT).show();
        }
    }
}

答案 1 :(得分:1)

您只是在创建文档,而不是在编写文档。

  Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TITLE, dst);
        startActivity(intent);

这将创建文档,并将文档的uri返回到您的应用。

现在您需要为此uri写一些内容,您将在onActivityResult中获得

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            try {
                Uri uri = data.getData();

                OutputStream outputStream = getContentResolver().openOutputStream(uri);

                outputStream.write("Hi, welcome to Sam's Android classroom! Have a good day!".getBytes());

                outputStream.close(); // very important
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}