如何使用NFC传输图像

发布于 2024-12-09 12:31:36 字数 2281 浏览 4 评论 0原文

我创建了一个简单的应用程序,其中有一个图像。现在我想做的是。

  1. 首先,我想向我的应用添加 NFC 支持。

  2. 在我的应用程序中添加 NFC 支持后,我想要的下一件事是如何将应用程序中存在的图像传输到另一台具有 NFC 支持的设备。

如果有人知道,请帮助我解决这个问题,如果可能的话,举个例子。我已阅读 developer.android.com 中提供的 NFC 文档,但在这种情况下,它只能使用 NFC 将文本从一台设备传输到另一台设备,但就我而言,我想传输图像而不是文本。

文本传输代码

public class NFCTestApp extends Activity 
{   
                                
   private NfcAdapter mAdapter;
   private TextView mText;
   private NdefMessage mMessage;

    public static NdefRecord newTextRecord(byte[] text, Locale locale, boolean enco   deInUtf8) 
    {
       byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));

       Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
       byte[] textBytes = text;

       int utfBit = encodeInUtf8 ? 0 : (1 << 7);
       char status = (char) (utfBit + langBytes.length);

       byte[] data = new byte[1 + langBytes.length + textBytes.length]; 
       data[0] = (byte) status;
       System.arraycopy(langBytes, 0, data, 1, langBytes.length);
       System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);

       return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
    }

    public void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
       mAdapter = NfcAdapter.getDefaultAdapter(this);

       setContentView(R.layout.main);

       
       Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       bm.compress(Bitmap.Compress.JPEG, 100, baos);
       byte[] b = baos.toByteArray();

      // Create an NDEF message 
       mMessage = new NdefMessage(
            new NdefRecord[] { newTextRecord(b, Locale.ENGLISH, true)});  
    }

    @Override
    public void onResume() 
    {
       super.onResume();
       if (mAdapter != null) mAdapter.enableForegroundNdefPush(this, mMessage);
    }

    @Override
    public void onPause() 
    {
       super.onPause();
       if (mAdapter != null) mAdapter.disableForegroundNdefPush(this);
    }
}

I have created a simple app where I have an image. Now what I want to do is.

  1. At first, I want to add NFC support to my app.

  2. Once I add NFC support in my app, the next thing I want is how can I transfer my image that is present in my app to another device having NFC support.

If anyone knows please help me to solve this out, if possible with an example. I have gone through the documentation as provided in developer.android.com for NFC, but in that case, it only gives transfer of text from one device to another using NFC, but in my case I want to transfer image instead of text.

Code for text transfer

public class NFCTestApp extends Activity 
{   
                                
   private NfcAdapter mAdapter;
   private TextView mText;
   private NdefMessage mMessage;

    public static NdefRecord newTextRecord(byte[] text, Locale locale, boolean enco   deInUtf8) 
    {
       byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));

       Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
       byte[] textBytes = text;

       int utfBit = encodeInUtf8 ? 0 : (1 << 7);
       char status = (char) (utfBit + langBytes.length);

       byte[] data = new byte[1 + langBytes.length + textBytes.length]; 
       data[0] = (byte) status;
       System.arraycopy(langBytes, 0, data, 1, langBytes.length);
       System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);

       return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
    }

    public void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
       mAdapter = NfcAdapter.getDefaultAdapter(this);

       setContentView(R.layout.main);

       
       Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       bm.compress(Bitmap.Compress.JPEG, 100, baos);
       byte[] b = baos.toByteArray();

      // Create an NDEF message 
       mMessage = new NdefMessage(
            new NdefRecord[] { newTextRecord(b, Locale.ENGLISH, true)});  
    }

    @Override
    public void onResume() 
    {
       super.onResume();
       if (mAdapter != null) mAdapter.enableForegroundNdefPush(this, mMessage);
    }

    @Override
    public void onPause() 
    {
       super.onPause();
       if (mAdapter != null) mAdapter.disableForegroundNdefPush(this);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2024-12-16 12:31:36

首先,您需要将其包含在清单文件中以启用 NFC 支持:

<uses-permission android:name="android.permission.NFC" />
<uses-sdk android:minSdkVersion="9" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

然后您可能会查看 NFCDemo,来自 Google 的官方 NFC 演示应用程序,供参考。

First, you need to include this in your manifest file to enable NFC support:

<uses-permission android:name="android.permission.NFC" />
<uses-sdk android:minSdkVersion="9" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />

Then you might have a look at NFCDemo, official NFC demo app from Google, for a reference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文