这些是下面定义的参考文献;
StorageReference strgRef;
FirebaseFirestore mfirestore;
ArrayList<String> files,status;
List<String> downloadedimages;
RecyclerView recview;
ImageView btn_upload;
myadapter adapter;
我有一个画廊意图,然后是activityforResults
从图库中选择多个图像,代码将所选图像列表上传到cloud firebase
存储,但我试图以 URI 的形式取回它,以便我可以发送这些 URI 来创建 firestore 集合,然后从 firestore 取回它以填充 imagesscroll 视图。[lets take an example of a product details page of amazon where multiple images of a product are displayed that is what I am trying to achieve with the above query]
需要注意的是,我想使用我的手机上传这些图像。
没有错误,来自 Uri successlistener 的代码块不起作用,它也不记录从 firebase storage 获取的 URI。我想从存储中存储这些 URI,然后将其放入云 Firestore 中,其中包含一个包含这些图像 Uri 列表的集合。我正在寻找一个解决方案,它将帮助我将这些 URI 存储到 arraylist 或列表中,稍后可以将其发送到 Firestore 就是这样。谢谢阅读! –
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 69 && resultCode == RESULT_OK) {
if (data.getClipData() != null) {
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
Uri fileuri = data.getClipData().getItemAt(i).getUri();
String filename = getfilenamefromuri(fileuri);
files.add(filename);
status.add("Loading");
adapter.notifyDataSetChanged();
final int index = i;
StorageReference upload = strgRef.child("newimages").child(filename);
upload.putFile(fileuri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
status.remove(index);
status.add(index, "upload completed");
adapter.notifyDataSetChanged();
//uris of stored images will be added to downloadimages list
StorageMetadata snapshotmetadata = taskSnapshot.getMetadata();
Task<Uri> downloadUrl = strgRef.child("newimages").getDownloadUrl();
downloadUrl.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
while (uri != null) {
final String imagesREf = uri.toString();
downloadedimages.add(imagesREf);
Toast.makeText(MainActivity2.this, "images loaded successfully", Toast.LENGTH_SHORT).show();
Log.e("Firbase retrieved urls", downloadedimages.toString());
}
}
});
}
});
}
}
}
}
StorageReference upload = strgRef.child("newimages").child(filename);
upload.putFile(fileuri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(MainActivity.this, "Upload successful", Toast.LENGTH_LONG).show();
Task<Uri> downloadUrl = strgRef.child("newimages").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
final String imagesREf = uri.toString();
//here, directly store uri in firebase database or in firestore.
mDatabaseRef.child("childName").setValue(imagesREf);
//or if you have model class use that to store in database
UploaModel upload = new UploadModel("imageUrl",
imagesREf);
mDatabaseRef.child("childName").setValue(upload);
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(UploadMake.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)