1 回答
TA贡献1757条经验 获得超8个赞
您需要taskSnapshot.getUploadSessionUri().toString()在将图像上传到正确的 uri 时进行更改,如下所示
下面是获取正确 URI 的示例
firebaseStorageReference.putFile(resultUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
final Task<Uri> firebaseUri = taskSnapshot.getStorage().getDownloadUrl();
firebaseUri.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String mDownloadUri = uri.toString();
}
});
}
});
并将其应用到您的代码中
private void uploadFile (){
if (mImageUri != null){
StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri));
uploadProgressBar.setVisibility(View.VISIBLE);
uploadProgressBar.setIndeterminate(true);
mUploadTask = fileReference.putFile(mImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
uploadProgressBar.setVisibility(View.VISIBLE);
uploadProgressBar.setIndeterminate(false);
uploadProgressBar.setProgress(0);
}
},500);
final Task<Uri> firebaseUri = taskSnapshot.getStorage().getDownloadUrl();
firebaseUri.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String downloadUri = uri.toString();
Toast.makeText(AddProductActivity.this, "Product is added successfully!", Toast.LENGTH_SHORT).show();
Product product = new Product(productName.getText().toString()
,productDescription.getText().toString()
,price.getText().toString()
,downloadUri
,oldPrice.getText().toString()
, quantity.getText().toString());
String uploadID = mDatabaseRef.push().getKey();
mDatabaseRef.child(uploadID).setValue(product);
uploadProgressBar.setVisibility(View.INVISIBLE);
openMainActivity();
}
});
// rest of your code
希望对您有帮助,也欢迎更多支持
添加回答
举报