我从电话联系人列表中选择了联系人,并且还找到了特定人员的姓名和电话号码,但是当我想将联系人发送到像WhatsApp这样的Firebase时,我找不到上传联系人的实际路径。为什么我找到了上述例外...
使用代码选择联系人
private static final int REQUEST_READ_CONTACT = 234;
private static String[] PERMISSIONS_CONTACT = {
Manifest.permission.READ_CONTACTS
};
int permission = ActivityCompat.checkSelfPermission(ChatActivity.this, Manifest.permission.READ_CONTACTS);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
ChatActivity.this,
PERMISSIONS_CONTACT,
REQUEST_READ_CONTACT
);
}else{
// we already have permission, lets go ahead and call camera intent
Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactIntent, PHONE_NUMBER_REQUEST);
}
onActivityResult代码:
if (requestCode==4){
if (resultCode==RESULT_OK){
String phoneNo = null;
String name = null;
Uri uri2=data.getData();
String uri = String.valueOf(data.getData());
Cursor cursor = getContentResolver().query(uri2, null, null, null, null);
if (cursor.moveToFirst()) {
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
UploadContactsToFireBase(uri,name,phoneNo);
}else {
Toast.makeText(getApplicationContext(),"Something went wrong",Toast.LENGTH_SHORT).show();
}
cursor.close();
}
UploadContactsToFireBase:
void UploadContactsToFireBase(String contactsPath, final String contactName, final String phoneNumber ){
final Dialogs loader=new Dialogs(this);
loader.showDialog(Dialogs.DIALOG_CENTERED);
final String name = DateFormat.format("yyyy-MM-dd_hhmmss", new Date()).toString();
StorageReference filePath = documentsStorageReference.child(name+"_contacts");
final Uri uri = Uri.fromFile(new File(contactsPath));
UploadTask uploadTask = filePath.putFile(uri);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
loader.dismiss();
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Toast.makeText(getApplicationContext(),"Download Url: "+downloadUrl,Toast.LENGTH_SHORT).show();
ContactModel contactModel = new ContactModel(downloadUrl.toString(),contactName,phoneNumber);
ChatMessage newMessage = new ChatMessage(null,mCurrentUserId,mRecipientId,
null,null,Calendar.getInstance().getTime().getTime()+"",null,null,null,contactModel);
messageChatDatabase.push().setValue(newMessage);
}
});
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG,"onFailure sendFileFirebase "+e.getMessage());
}
});
}
错误:
10-31 13:45:33.632 11777-15995/com.tecorb.chatapp E/StorageException: /content:/com.android.contacts/data/1: open failed: ENOENT (No such file or directory)
java.io.FileNotFoundException: /content:/com.android.contacts/data/1: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at android.content.ContentResolver.openInputStream(ContentResolver.java:644)
at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
at com.tecorb.chatapp.ui.ChatActivity.uploadContacts(ChatActivity.java:927)
at com.tecorb.chatapp.ui.ChatActivity.onActivityResult(ChatActivity.java:639)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:442)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at java.io.FileInputStream.<init>(FileInputStream.java:103)
at android.content.ContentResolver.openInputStream(ContentResolver.java:644)
at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
at com.tecorb.chatapp.ui.ChatActivity.uploadContacts(ChatActivity.java:927)
at com.tecorb.chatapp.ui.ChatActivity.onActivityResult(ChatActivity.java:639)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)