如何使用NFC传输图像
我创建了一个简单的应用程序,其中有一个图像。现在我想做的是。
首先,我想向我的应用添加 NFC 支持。
在我的应用程序中添加 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.
At first, I want to add NFC support to my app.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要将其包含在清单文件中以启用 NFC 支持:
然后您可能会查看 NFCDemo,来自 Google 的官方 NFC 演示应用程序,供参考。
First, you need to include this in your manifest file to enable NFC support:
Then you might have a look at NFCDemo, official NFC demo app from Google, for a reference.