有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法将图像上载到firebase

我正在尝试使用firebase上载图像,但我无法这样做。我没有得到什么是错误的,而上传。代码如下:

    private void saveUserInformation() {
            mCustomerDatabase.updateChildren(userInfo);
            if (resultUri != null) {
                final StorageReference filepath = FirebaseStorage.getInstance().getReference().child("profileImages").child(userId);
                Bitmap bitmap = null;
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getApplication().getContentResolver(), resultUri);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                assert bitmap != null;
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] data = baos.toByteArray();
                UploadTask uploadTask = filepath.putBytes(data);
                uploadTask.addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("SA", "Fail1");
                        finish();
                    }
                });
                uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                Map newImage = new HashMap();
                                newImage.put("profileImageUrl", uri.toString());
                                mCustomerDatabase.updateChildren(newImage);
                                Log.d("SA", "Success");
                                finish();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception exception) {
                                Log.e("SA", "Fail2");
                                finish();
                            }
                        });
                    }
                });
             } else {
                   finish();
             }
        }

发生StorageException,并且它也不会上载到firebase控制台中。谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    定义变量

    private static int GALLERY_PICK = 1 ;
    private Uri imageUri;
    private Button uploudBtn;
    private ImageView profileImageView;
    

    按下imageView时,从库中选择图像

    profileImageView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent galleryIntent = new Intent();
                    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                    galleryIntent.setType("image/*");
                    startActivityForResult(galleryIntent , GALLERY_PICK);
                }
            });
    

    onActivityResult方法

    @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
        {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == GALLERY_PICK && resultCode == RESULT_OK && data != null)
            {
                imageUri = data.getData();
                profileImageView.setImageURI(imageUri);
            }
        }
    

    然后是图像的上传代码(按钮)

    uploudBtn.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
    
                    if(imageUri != null)
                       {
                         final StorageReference filePath = userProfileImageRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
                final UploadTask uploadTask = filePath.putFile(imageUri);
                uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>()
                {
                    @Override
                    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
                    {
                        if (!task.isSuccessful())
                        {
                            throw  task.getException();
                        }
                        downloadUrl = filePath.getDownloadUrl().toString();
                        return filePath.getDownloadUrl();
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>()
                {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task)
                    {
                        if (task.isSuccessful())
                        {
                            downloadUrl = task.getResult().toString();
                          Toast.makeText(MainActivity.this, "Your profile Info has been updated", Toast.LENGTH_SHORT).show();
    
                       }
    
                }
            });
    

    **备注您应该在OnCreate方法中强制转换uploudBtn、profileImageView**

    profileImageView = findViewById(R.id.imageView);
    uploudBtn = findViewById(R.id.button);