我正在制作一个聊天应用程序,在这里我们可以将文件上传到firebase,就像选择图片选择器一样,如果我们选择图片,然后单击它,然后选择并发送并聊天聊天图片,例如whatsapp。 照片选择器的意图工作正常,但是当我选择图像时,它无法发送图像,因此无法上传
触发光电选择器的代码意图:
mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO: Fire an intent to show an image picker
Log.d(TAG, "onClick: is it working?");
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY,true);
startActivityForResult(Intent.createChooser(intent,"Complete action using"),RC_PHOTO_PICKER);
}
});
拾取要上传的照片的代码是:
else if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){
Uri selectedImageUri = data.getData();
StorageReference photoRef = mStorageReference.child(selectedImageUri.getLastPathSegment());
//upload file to firebase storage
photoRef.putFile(selectedImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
FriendlyMessage friendlyMessage = new FriendlyMessage(null,mUsername,downloadUrl.toString());
mMessageDatabaseReference.push().setValue(friendlyMessage);
}
});
FriendlyMessage.java
包com.google.firebase.udacity.friendlychat;
public class FriendlyMessage {
private String text;
private String name;
private String photoUrl;
public FriendlyMessage() {
}
public FriendlyMessage(String text, String name, String photoUrl) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
}
Logcat:
.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-21 11:56:08.401 9124-9401/com.google.firebase.udacity.friendlychat I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
04-21 11:56:08.401 9124-9401/com.google.firebase.udacity.friendlychat I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 6
04-21 11:56:08.410 9124-9327/com.google.firebase.udacity.friendlychat I/FA: Tag Manager is not found and thus will not be used
04-21 11:56:08.440 9124-9404/com.google.firebase.udacity.friendlychat D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-21 11:56:08.446 9124-9401/com.google.firebase.udacity.friendlychat W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/0000002d/n/armeabi-v7a
04-21 11:56:08.447 9124-9401/com.google.firebase.udacity.friendlychat W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/0000002d/n/armeabi
04-21 11:56:08.466 9124-9124/com.google.firebase.udacity.friendlychat D/FirebaseApp: Notifying auth state listeners.
04-21 11:56:08.466 9124-9124/com.google.firebase.udacity.friendlychat D/FirebaseApp: Notified 0 auth state listeners.
04-21 11:56:08.519 9124-9404/com.google.firebase.udacity.friendlychat I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: (Ifd751822f5)
OpenGL ES Shader Compiler Version: XE031.06.00.05
Build Date: 01/26/16 Tue
Local Branch: AU12_SBA
Remote Branch:
Local Patches:
Reconstruct Branch:
04-21 11:56:08.521 9124-9404/com.google.firebase.udacity.friendlychat I/OpenGLRenderer: Initialized EGL, version 1.4
04-21 11:56:08.887 9124-9124/com.google.firebase.udacity.friendlychat W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
04-21 11:56:09.110 9124-9404/com.google.firebase.udacity.friendlychat V/RenderScript: 0xb7aa3c00 Launching thread(s), CPUs 4
存储数据库规则:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
是的,我发现了问题
将FriendlyMessageModel类的所有成员公开!
public class FriendlyMessage {
public String text;
public String name;
public String photoUrl;
public FriendlyMessage() {
}
public FriendlyMessage(String text, String name, String photoUrl) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
}