【发布时间】:2020-03-16 21:26:48
【问题描述】:
这是我到目前为止所能得到的。选择图片后
selectImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mUploadTask != null && mUploadTask.isInProgress()){
Toast.makeText(MainActivity.this, "Upload In Progress", Toast.LENGTH_SHORT).show();
}
else {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE);
}
}
});
在OnActivityResult 中,我正在将我选择的图片上传到 Firebase 存储,同时我想将这些多张图片的下载 URL 存储到 Firebase Firestore
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String fileName;
final String[] downloadUrl = new String[1];
if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
if(data.getClipData() != null){
int totalItemsSelected = data.getClipData().getItemCount();
for(int i =0;i<totalItemsSelected;i++){
Uri fileUri = data.getClipData().getItemAt(i).getUri();
fileName = getFileName(fileUri);
fileNameList.add(fileName);
fileDoneList.add("uploading");
uploadListAdapter.notifyDataSetChanged();
final StorageReference fileToUpload = storageReference.child("Images").child(fileName);
final int finalI = i;
final int totalCount = totalItemsSelected;
mUploadTask = fileToUpload.putFile(fileUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileToUpload.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = String.valueOf(uri);
storeLink(url,totalCount);//Method To Store the Url
}
});
// Toast.makeText(add_for_sale.this, "Uploading works", Toast.LENGTH_SHORT).show();
fileDoneList.remove(finalI);
fileDoneList.add(finalI,"done");
uploadListAdapter.notifyDataSetChanged();
}
});
// ImageUploadInfo imageUploadInfo = new ImageUploadInfo(downloadUrl[0],fileName);
}
Toast.makeText(this, "Upload in Progress", Toast.LENGTH_SHORT).show();
}
else if(data.getData() != null){
Toast.makeText(this, "Selected Single", Toast.LENGTH_SHORT).show();
}
}
}
在storeLink(url,totalCount) 方法中,我正在创建一个地图对象以在文档中创建字段“imageUrl”,其中“29t0Boxm0fa8UNkomuo5xPLwkx13”是一个用户ID。
private void storeLink(final String url,final int totalCount) {
FirebaseFirestore storeUrl = FirebaseFirestore.getInstance();
Toast.makeText(MainActivity.this, url, Toast.LENGTH_LONG).show();
for (int i=0;i<totalCount;i++) {
final Map<String, Object> image = new HashMap<>();
image.put("imageUrl"+i, url);
DocumentReference documentReference = storeUrl.collection("users").document("29t0Boxm0fa8UNkomuo5xPLwkx13");
documentReference.set(image).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "" +
"OnSuccess : Image Link Saved ");
// startActivity(new Intent(Register.this,LoginActivity.class));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "OnFailure : " + e.toString());
}
});
}
}
存储规则
rules_version = '2'
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
Firestore Rules
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
【问题讨论】:
标签: java android firebase google-cloud-firestore google-cloud-storage