我想从imageview而不是图库或文件夹中选择图像。这样做的原因是,图像被放置在imageview中是从图库或服务器,或者从android studio内部。由于我不知道如何将图像放置在imageview中,因此我想将图像从imageview发送到Firebase存储。
答案 0 :(得分:1)
假设身份验证过程成功,要将图像的链接存储在Firebase实时数据库中,请使用以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
usersRef.child(uid).child("downloadURL").setValue(downloadURL);
数据库中的结果将是:
Firebase-root
|
--- users
|
--- uid
|
---downloadURL: "https://..."
答案 1 :(得分:0)
我之前做过类似的事情,这是我使用的代码
//Start Uploading to RealTime Database
StorageReference filepath = mStorageReference.child("Blog_Images").child(mImageURI.getLastPathSegment());
filepath.putFile(mImageURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadURL = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabaseReference.push();
Map<String, String> dataToSave = new HashMap<>();
dataToSave.put("title", titleString);
dataToSave.put("description", descString);
dataToSave.put("image", downloadURL.toString());
dataToSave.put("timeStamp", String.valueOf(java.lang.System.currentTimeMillis()));
dataToSave.put("userID", mUser.getUid());
newPost.setValue(dataToSave);
mProgressDialog.dismiss();
startActivity(new Intent(AddPostActivity.this, PostListActivity.class));
finish();
}
有关完整代码,请访问save a picture to firebase database