无法在 FirebaseStorage 上上传图片
Posted
技术标签:
【中文标题】无法在 FirebaseStorage 上上传图片【英文标题】:Cannot upload image on FirebaseStorage 【发布时间】:2021-07-22 23:55:42 【问题描述】:我无法在 Firebase 存储上上传个人资料图片。 我正在从吐司中打印错误。 为什么会这样?我无法解决问题,我多次查看我的代码。 可能是什么问题,我该如何调整我的代码以使其正常工作? 提前感谢大家。
public class ProfiloUtenteActivity extends AppCompatActivity
private ImageView mImageBtn;
private ImageView mSettingsBtn, mPersonalReviewsBtn, mShowFriendsBtn;
private CircleImageView mProfileImage;
private TextView mMailAcc, mUsernameAcc;
private static final int GALLERY_PICK = 1;
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
private ProgressDialog mProgress;
private StorageReference mImageStorage;
private String myUri = "";
private Uri imageUri;
FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profilo_utente);
mShowFriendsBtn = findViewById(R.id.imageShowFriends);
mPersonalReviewsBtn = findViewById(R.id.imagePersonalReviews);
mSettingsBtn = (ImageView) findViewById(R.id.imageViewSettings);
mImageBtn = (ImageView) findViewById(R.id.change_image_btn);
mProfileImage = (CircleImageView) findViewById(R.id.profile_image);
mMailAcc = (TextView) findViewById(R.id.textViewMailAcc);
mUsernameAcc = (TextView) findViewById(R.id.textView7);
auth = FirebaseAuth.getInstance();
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
String current_uid = mCurrentUser.getUid();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("utenti").child(current_uid);
mImageStorage = FirebaseStorage.getInstance().getReference();
mImageBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent, "Seleziona una foto"), GALLERY_PICK);
);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_PICK && resultCode == RESULT_OK)
imageUri = data.getData();
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(this);
if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
CropImage.ActivityResult result = CropImage.getActivityResult(data);
Uri resultUri = result.getUri();
StorageReference filepath = mImageStorage.child("profile_images").child("1wq");
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>()
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task)
if(task.isSuccessful())
Toast.makeText(ProfiloUtenteActivity.this, "Funziona", Toast.LENGTH_SHORT).show();
else
Toast.makeText(ProfiloUtenteActivity.this, "Error", Toast.LENGTH_SHORT).show();
);
【问题讨论】:
【参考方案1】:当Task
失败时,它包含一个异常,其中包含有关失败的更多信息。可让您找出问题原因的日志记录:
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task)
if(task.isSuccessful())
Toast.makeText(ProfiloUtenteActivity.this, "Funziona", Toast.LENGTH_SHORT).show();
else
Toast.makeText(ProfiloUtenteActivity.this, "Error", Toast.LENGTH_SHORT).show();
Log.e("Upload", "Upload failed", task.getException());
一旦您收到错误消息,我建议您搜索它可能已经有人处理过。
【讨论】:
谢谢。在 logcat 中,它显示错误代码 403 权限被拒绝。在 Firebase 存储中,我的规则如下(使用此规则,它仍然表示权限被拒绝)。rules_version = '2'; service firebase.storage match /b/test-b-default-rtdb.firebaseio.com/o match /allPaths=** // Allow access by all users allow read, write;
以上是关于无法在 FirebaseStorage 上上传图片的主要内容,如果未能解决你的问题,请参考以下文章
我应该将图片上传到 Cloud Firestore 还是 Firebase Storage?
从手机上传文件时,Firebase 存储(网络)无法正常工作
TypeError:无法读取未定义的属性“instanceIdentifier”(FirebaseStorage)
身份验证在 FirebaseStorage Flutter 中不起作用