2

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.

  • Please give some examples of filenames. If they are in the public Download folder (see comment in your code) then why are you suggesting they are in a private folder in the Android directory. Further: if they are indeed in a private folder in the Android/data directory then they are out of reach for the mediastore. Please find out where your files are. – blackapps Mar 17 '21 at 09:45
  • Files are stored under Nearby's downloads under the file path /storage/emulated/0/Download/Nearby/ and then the filename is the Payload ID so would be something like -6159828823084046368 – Mark Morrison Mar 17 '21 at 09:59
  • `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.` All has nothing to do with scoped storage. Your app can just try to list als files in the Nearby folder with `new File( "/storage/emulated/0/Download/Nearby").list();` Did you try that already? If you can you can use new FileInputStream to read the files. Does the Files app display those files from ./Download/Nearby ? You can also query the MediaStore for RELATIVE_PATH Download/Nearby. – blackapps Mar 17 '21 at 10:44
  • 2
    The Nearby folder is being renamed to ".nearby" in an update that's rolling out over the next 2 weeks. Looking it up manually is a workaround, until the SDK can be updated. – Xlythe Mar 18 '21 at 04:15

1 Answers1

2

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

Xlythe
  • 1,958
  • 1
  • 8
  • 12