How to receive file using NFC (Android Beam) in Android

1.3k views Asked by At

I was able to send a file using NFC, based on tutorials on the Android developer site. However I'm unable to handle the receiver part.

I follow http://developer.android.com/training/beam-files/receive-files.html for the receiving side and I get the notification that the Beam file transfer was successful on the receiver. When the user clicks this notification, I expect the that my app should be launched.

My receiving activity has the following intent filters:

<intent-filter>
 <action android:name="android.intent.action.VIEW"/>
 <category android:name="android.intent.category.DEFAULT"/>
 <data android:mimeType="image/*" />
 <data android:mimeType="video/*" />
 <data android:scheme="file" />
</intent-filter>

But my receiving activity never gets called even the file transfer was finished. How can I receive the file in my app?

1

There are 1 answers

0
Michael Roland On

From Receiving Files from Another Device:

Note: For Android Beam file transfer, you receive a content URI in the ACTION_VIEW intent if the first incoming file has a MIME type of "audio/*", "image/*", or "video/*", indicating that the file is media- related.

Due to the way how <data ... /> filters are processed (see Data Test and Data Element), your intent filter translates to

  • intent action VIEW and MIME type "image/*" and URI with scheme "file:", or
  • intent action VIEW and MIME type "video/*" and URI with scheme "file:".

So it must match any of the MIME types and any of the URIs that are given in the data element(s)..

Consequently your intent filter can never match as both, the "image/*" MIME type and the "video/*" MIME type will result in a content URI and not a "file:" URI. Hence, either skipping the URI filter part or chaging the filtered scheme to "content" should do the trick.

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="image/*" />
    <data android:mimeType="video/*" />
</intent-filter>

or

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="image/*" />
    <data android:mimeType="video/*" />
    <data android:scheme="content" />
</intent-filter>