Search code examples
androidnfcnfc-p2p

NFC send image/jpeg


How can I send an image/jpeg from my app that uses NFC? According to the Android documentation, the NdefRecord createMime can do it for me. I have only used createUri for passing a URL to another Android device. But I am not sure how I can get started on converting my jpeg/image to an NdefRecord. It seems like I need to convert it into bytes.


Solution

  • Just got the answer to my question. What I was doing before was:

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG,100,stream);
    byte[] byteArray = stream.toByteArray();
    
    NdefMessage img = new NdefMessage(NdefRecord.createMime("Test Image", byteArray));
    

    And the result to this was a tag result of a string "Test Image". What I did was change the "Test Image" to "image/jpeg" and the result is the image was received on the other device. Only downside is from less than an hour of testing, sometimes a 2-3 KB image takes about 30 seconds the same as a 30 KB image.