narb
narb

Reputation: 988

SAF - ACTION_CREATE_DOCUMENT - after I save the file on Drive the file is empty

I am a beginner in SAF. What I'm trying to do is super simple to save a config. Let's say the file is .conf.

I copy .conf to conf.txt and I save it on Drive.

Here is my code:

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

I do a save in Drive. The file appears on my pc but when I open it, it's empty.

When I do the inverse: 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();
                }

The function readFile opens the file and stops while reading because there is no data.

What did I do wrong?

Upvotes: 2

Views: 2938

Answers (2)

Sam Chen
Sam Chen

Reputation: 8847

The Intent(Intent.ACTION_CREATE_DOCUMENT) is for CREATING text file, and use onActivityResult() to get the uri (location) from the file, THEN you use OutputStream to WRITE data (byte[]) to the file.


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

Upvotes: 5

Mayank Kumar Chaudhari
Mayank Kumar Chaudhari

Reputation: 18528

You are just creating the document but not writing it.

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

this will create document and return the uri of the document to your app.

Now you need to write something to this uri that you will get in 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();
            }
        }
    }
}

Upvotes: 0

Related Questions