在将图像上传到Firebase存储中时出错

时间:2020-07-01 17:03:30

标签: android firebase firebase-realtime-database firebase-storage

我想将一些用户数据及其个人资料图片添加到firebase。该应用程序包含用于从图库中选择个人资料图片或使用相机获取图片的选项。 我可以通过两种方式在Imageview中获取图片。但是在将其上传到Firebase存储中时出现以下错误。

“发生未知错误,请检查HTTP结果代码和服务器响应的内部异常”

从相机或图库中获取图像并将其放置到imageview中的方法

 private void addImage(Context context) {
    final CharSequence[] options = {"Use Camera","Choose from Gallery","cancel"};
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Choose your profile picture");
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if(options[item].equals("Use Camera")){
                Intent clickPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(clickPicture,0);
            }
            else if (options[item].equals("Choose from Gallery")){
                Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(pickPhoto,1);
            }
            else if (options[item].equals("cancel")){
                dialog.dismiss();
            }}});builder.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode != RESULT_CANCELED){
        switch (requestCode){
            case 0:
                if(resultCode ==RESULT_OK && data != null){
                    filepath = data.getData();
                    Bitmap selectedImage = (Bitmap) data.getExtras().get("data");
                  profileImage.setImageBitmap(selectedImage);
                }
                break;
            case 1 :
                if(resultCode ==RESULT_OK && data != null) {
                    filepath = data.getData();

                    try {
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filepath);
                        profileImage.setImageBitmap(bitmap);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
        } } }

用于在Firebase中存储数据的代码

if(!TextUtils.isEmpty(name)&& !TextUtils.isEmpty(location) && !TextUtils.isEmpty(address)&&!TextUtils.isEmpty(experience)
        &&!TextUtils.isEmpty(time) && filepath !=null){
storageReference.putFile(filepath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String ImageUrl = taskSnapshot.getStorage().getDownloadUrl().toString();

                            id = reference.push().getKey();
                            Data data = new Data(name,speciality,quaification,experience,fees,timming,ImageUrl);
                            reference.child(id).setValue(data).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if(task.isSuccessful()){
                                        Toast.makeText(Doc_Basic_Activity.this,"Details Saved",Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(Doc_Basic_Activity.this,Doc_ProfilePage.class);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();                    }
                                    else{
                                        Toast.makeText(Doc_Basic_Activity.this,"Failed to save details",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
   Toast.makeText(Doc_Basic_Activity.this,e.getMessage(),Toast.LENGTH_LONG).show();
    }
});

0 个答案:

没有答案