Mark Morrison
Mark Morrison

Reputation: 43

How to access file that has been transferred using Nearby Connections API with android 11, as file is stored in Nearby's scoped storage?

I am trying to transfer a file using googles Nearby Connections API. Largely I can get all components of the transfer to work so that all of the files data is transferred but the issue is that the files data is then stored in Nearby's scoped storage so I am unable to access it from my app to be able to process that data into the appropriate file type and then save and rename as necessary.

The current method I am using to try and process the payload is

private void processFilePayload(long payloadId) {

// BYTES and FILE could be received in any order, so we call when either the BYTES or the FILE
// payload is completely received. The file payload is considered complete only when both have
// been received.
    Payload filePayload = completedFilePayloads.get(payloadId);
    String filename = filePayloadFilenames.get(payloadId);

    if (filePayload != null && filename != null) {
        completedFilePayloads.remove(payloadId);
        filePayloadFilenames.remove(payloadId);

// Get the received file (which will be in the Downloads folder)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

              File fromPayload = filePayload.asFile().asJavaFile();
              Uri uri = Uri.fromFile(fromPayload);

              try {
              // Copy the file to a new location.
                   InputStream in = getApplicationContext().getContentResolver().openInputStream(uri);
                   copyStream(in, new FileOutputStream(new File(getApplicationContext().getCacheDir(), filename)));
                  } catch (IOException e) {
                      // Log the error.
                      Log.e("copy file", e.toString());
                  } finally {
                      // Delete the original file.
                      getApplicationContext().getContentResolver().delete(uri, null, null);
                  }

            } else  {
            File payloadFile = filePayload.asFile().asJavaFile();

            // Rename the file.
            payloadFile.renameTo(new File(payloadFile.getParentFile(), filename));
            }
        }
    }

});

Because of android 11's scoped storage to be able to access the file the files Uri needs to be used to create an input stream with a content resolver to access the file.

According to the Nearby Documentation there should be a method Payload.File.asUri so I would be able to use the line Uri payloadUri = filePayload.asFile().asUri(); but this is not actually available in the API despite using the most recent version of Nearby.

As well as this the use of Payload.File.AsJavaFile() should be deprecated according to the google Nearby documentation

I have seen some other answers for similar problems where the suggestion is to use Media.Store but this is not possible as the file does not have any extension yet so doesn't show up as any particular file type.

Note: I have requested read/write external storage permissions both in the manifest and at runtime.

Upvotes: 2

Views: 721

Answers (1)

Xlythe
Xlythe

Reputation: 2033

===Update===

Payload.asFile.asUri() is available in com.google.android.gms:play-services-nearby:18.0.0

============

Sorry about that. We'll be releasing an update soon with #asUri properly exposed.

In the meantime, if you target API 29, you can use requestLegacyExternalStorage=true as a workaround. (See more: https://developer.android.com/about/versions/11/privacy/storage)

Upvotes: 2

Related Questions