Java Can';t使用NFC发送和发送图像
我有一种方法,当我点击一个按钮时,通过NFC向另一部智能手机发送图像。代码看起来是正确的,执行到达代码的最后一行,但什么也没有发生。两款智能手机都有NFC和Android Beam。代码如下:Java Can';t使用NFC发送和发送图像,java,android,nfc,Java,Android,Nfc,我有一种方法,当我点击一个按钮时,通过NFC向另一部智能手机发送图像。代码看起来是正确的,执行到达代码的最后一行,但什么也没有发生。两款智能手机都有NFC和Android Beam。代码如下: public void sendFile(View view){ nfcAdapter=NfcAdapter.getDefaultAdapter(this); if(!nfcAdapter.isEnabled()){ Toast.makeText(this,"Please
public void sendFile(View view){
nfcAdapter=NfcAdapter.getDefaultAdapter(this);
if(!nfcAdapter.isEnabled()){
Toast.makeText(this,"Please enable NFC",Toast.LENGTH_SHORT).show();
startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
} else if (!nfcAdapter.isNdefPushEnabled()) {
Toast.makeText(this,"Please enable android beam",Toast.LENGTH_SHORT).show();
startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
} else {
//If we got here, Nfc and Android beam are enabled
Toast.makeText(this,"Ok",Toast.LENGTH_SHORT).show();
String fileName="test.jpg";
File fileDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File fileToTransfer =new File(fileDirectory,fileName);
Toast.makeText(this,"Tutto Ok2",Toast.LENGTH_SHORT).show();
fileToTransfer.setReadable(true,false);
Uri fileUri=Uri.fromFile(fileToTransfer);
if(fileUri==null){
Toast.makeText(this,"File not found",Toast.LENGTH_SHORT).show();
} else{
//If we got here, the app has found the image and is ready to send it
Toast.makeText(this,"Tutto Ok3",Toast.LENGTH_SHORT).show();
//Send the image
nfcAdapter.setBeamPushUris(new Uri[]{Uri.fromFile(fileToTransfer)},this);
}
}
如果有任何帮助,我们将不胜感激。波束通信不能一次点击就启动 见官方文件: 此功能有一个简单的API,允许用户启动传输 只需触摸设备即可完成此过程 一个设备必须接触另一个设备才能激活波束通信。 您可以允许在点击后进行光束传输,但必须触摸设备 请阅读:
public class MainActivity extends Activity {
NfcAdapter mNfcAdapter;
// Flag to indicate that Android Beam is available
boolean mAndroidBeamAvailable = false;
// Instance that returns available files from this app
private FileUriCallback mFileUriCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
// NFC isn't available on the device
if (!PackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)){
/*
* Disable NFC features here.
* For example, disable menu items or buttons that activate
* NFC-related features
*/
...
// Android Beam file transfer isn't supported
} else if (Build.VERSION.SDK_INT <
Build.VERSION_CODES.JELLY_BEAN_MR1) {
// If Android Beam isn't available, don't continue.
mAndroidBeamAvailable = false;
/*
* Disable Android Beam file transfer features here.
*/
...
// Android Beam file transfer is available, continue
} else {
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
/*
* Instantiate a new FileUriCallback to handle requests for
* URIs
*/
mFileUriCallback = new FileUriCallback();
// Set the dynamic callback for URI requests.
mNfcAdapter.setBeamPushUrisCallback(mFileUriCallback,this);
}
}
/**
* Callback that Android Beam file transfer calls to get
* files to share
*/
private class FileUriCallback implements
NfcAdapter.CreateBeamUrisCallback {
public FileUriCallback() {
}
/**
* Create content URIs as needed to share with another device
*/
@Override
public Uri[] createBeamUris(NfcEvent event) {
// List of URIs to provide to Android Beam
Uri[] mFileUris = new Uri[1];
String transferFile = "transferimage.jpg";
File extDir = getExternalFilesDir(null);
File requestFile = new File(extDir, transferFile);
requestFile.setReadable(true, false);
// Get a URI for the File and add it to the list of URIs
fileUri = Uri.fromFile(requestFile);
if (fileUri != null) {
mFileUris[0] = fileUri;
} else {
Log.e("My Activity", "No File URI available for file.");
}
return mFileUris;
}
}
}
公共类MainActivity扩展活动{
NfcAdapter mNfcAdapter;
//指示Android Beam可用的标志
布尔mAndroidBeamAvailable=false;
//从该应用程序返回可用文件的实例
私有FileUriCallback mFileUriCallback;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//NFC在设备上不可用
if(!PackageManager.hasSystemFeature(PackageManager.FEATURE\u NFC)){
/*
*在这里禁用NFC功能。
*例如,禁用激活的菜单项或按钮
*NFC相关功能
*/
...
//不支持Android Beam文件传输
}else if(Build.VERSION.SDK_INT<
Build.VERSION\u code.JELLY\u BEAN\u MR1){
//如果Android Beam不可用,请不要继续。
mAndroidBeamAvailable=false;
/*
*在此处禁用Android Beam文件传输功能。
*/
...
//Android Beam文件传输可用,是否继续
}否则{
mNfcAdapter=NfcAdapter.getDefaultAdapter(此);
/*
*实例化一个新的FileUriCallback来处理
*URI
*/
mFileUriCallback=newfileuricallback();
//设置URI请求的动态回调。
mNfcAdapter.setbeampushuricallback(mFileUriCallback,this);
}
}
/**
*Android Beam文件传输调用以获取的回调
*要共享的文件
*/
私有类FileUri回调实现
NfcAdapter.CreateBeamUrisCallback{
公共FileUriCallback(){
}
/**
*根据需要创建内容URI以与其他设备共享
*/
@凌驾
公共Uri[]CreateBeamURI(NfcEvent事件){
//要提供给Android Beam的URI列表
Uri[]mFileUris=新Uri[1];
字符串transferFile=“transferimage.jpg”;
File extDir=getExternalFilesDir(null);
文件请求文件=新文件(extDir,transferFile);
setReadable(true、false);
//获取文件的URI并将其添加到URI列表中
fileUri=Uri.fromFile(requestFile);
if(fileUri!=null){
mFileUris[0]=fileUri;
}否则{
Log.e(“我的活动”,“没有可用于文件的文件URI”);
}
返回mFileUris;
}
}
}
一次点击无法启动波束通信
见官方文件:
此功能有一个简单的API,允许用户启动传输
只需触摸设备即可完成此过程
一个设备必须接触另一个设备才能激活波束通信。
您可以允许在点击后进行光束传输,但必须触摸设备
请阅读:
public class MainActivity extends Activity {
NfcAdapter mNfcAdapter;
// Flag to indicate that Android Beam is available
boolean mAndroidBeamAvailable = false;
// Instance that returns available files from this app
private FileUriCallback mFileUriCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
// NFC isn't available on the device
if (!PackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)){
/*
* Disable NFC features here.
* For example, disable menu items or buttons that activate
* NFC-related features
*/
...
// Android Beam file transfer isn't supported
} else if (Build.VERSION.SDK_INT <
Build.VERSION_CODES.JELLY_BEAN_MR1) {
// If Android Beam isn't available, don't continue.
mAndroidBeamAvailable = false;
/*
* Disable Android Beam file transfer features here.
*/
...
// Android Beam file transfer is available, continue
} else {
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
/*
* Instantiate a new FileUriCallback to handle requests for
* URIs
*/
mFileUriCallback = new FileUriCallback();
// Set the dynamic callback for URI requests.
mNfcAdapter.setBeamPushUrisCallback(mFileUriCallback,this);
}
}
/**
* Callback that Android Beam file transfer calls to get
* files to share
*/
private class FileUriCallback implements
NfcAdapter.CreateBeamUrisCallback {
public FileUriCallback() {
}
/**
* Create content URIs as needed to share with another device
*/
@Override
public Uri[] createBeamUris(NfcEvent event) {
// List of URIs to provide to Android Beam
Uri[] mFileUris = new Uri[1];
String transferFile = "transferimage.jpg";
File extDir = getExternalFilesDir(null);
File requestFile = new File(extDir, transferFile);
requestFile.setReadable(true, false);
// Get a URI for the File and add it to the list of URIs
fileUri = Uri.fromFile(requestFile);
if (fileUri != null) {
mFileUris[0] = fileUri;
} else {
Log.e("My Activity", "No File URI available for file.");
}
return mFileUris;
}
}
}
公共类MainActivity扩展活动{
NfcAdapter mNfcAdapter;
//指示Android Beam可用的标志
布尔mAndroidBeamAvailable=false;
//从该应用程序返回可用文件的实例
私有FileUriCallback mFileUriCallback;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//NFC在设备上不可用
if(!PackageManager.hasSystemFeature(PackageManager.FEATURE\u NFC)){
/*
*在这里禁用NFC功能。
*例如,禁用激活的菜单项或按钮
*NFC相关功能
*/
...
//不支持Android Beam文件传输
}else if(Build.VERSION.SDK_INT<
Build.VERSION\u code.JELLY\u BEAN\u MR1){
//如果Android Beam不可用,请不要继续。
mAndroidBeamAvailable=false;
/*
*在此处禁用Android Beam文件传输功能。
*/
...
//Android Beam文件传输可用,是否继续
}否则{
mNfcAdapter=NfcAdapter.getDefaultAdapter(此);
/*
*实例化一个新的FileUriCallback来处理
*URI
*/
mFileUriCallback=newfileuricallback();
//设置URI请求的动态回调。
mNfcAdapter.setbeampushuricallback(mFileUriCallback,this);
}
}
/**
*Android Beam文件传输调用以获取的回调
*要共享的文件
*/
私有类FileUri回调实现
NfcAdapter.CreateBeamUrisCallback{
公共FileUriCallback(){
}
/**
*根据需要创建内容URI以与其他设备共享
*/
@凌驾
公共Uri[]CreateBeamURI(NfcEvent事件){
//要提供给Android Beam的URI列表
Uri[]mFileUris=新Uri[1];
字符串transferFile=“transferimage.jpg”;
File extDir=getExternalFilesDir(null);
File requestFile=新文件(extDir,transferFile