无法将图像上传到Firebase云存储

问题描述 投票:2回答:1

我正在尝试将图像上传到Firebase Cloud Storage。但是,每当我去选择一个图像时,我都会进行裁剪操作,然后我又回到上一个活动。我拥有所有读写权限,但仍然无法上传图片。

而且它还说这行总是假的

if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)

我的代码

Profile_beta.java

  public class Profile_beta extends AppCompatActivity {

    CircleImageView image_profile;
    TextView username;
    MaterialButton uploadButon;

    DatabaseReference reference;
    FirebaseUser fuser;
    StorageReference storageReference;

    private static final int PICK_IMAGE_REQUEST = 1;
    private  int Storage_Permission_Req=1;
    private StorageTask uploadTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_beta);

        image_profile =findViewById(R.id.profile_image);
        username = findViewById(R.id.username);


        storageReference = FirebaseStorage.getInstance().getReference("uploads");

        //fuser = FirebaseAuth.getInstance().getCurrentUser();
        //reference = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());

        image_profile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(Profile_beta.this,
                        Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){
                    openFileChooser();
                }
                else{
                    requestStoragePermission();
                }
            }
        });
    }

    private void openFileChooser() {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, PICK_IMAGE_REQUEST);

    }

    private void requestStoragePermission() {
        ActivityCompat.requestPermissions(Profile_beta.this,new String[]
                {Manifest.permission.READ_EXTERNAL_STORAGE},Storage_Permission_Req);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if(requestCode==Storage_Permission_Req){
            if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(Profile_beta.this,"Permission Granted",Toast.LENGTH_SHORT).show();
                openFileChooser();
            }
            else {
                Toast.makeText(Profile_beta.this,"Permission Not Granted",Toast.LENGTH_SHORT).show();
            }

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                && data != null ) {
            Uri ImageUri = data.getData();

            CropImage.activity(ImageUri)
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setAspectRatio(1,1)
                    .start(this);

            if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {

                CropImage.ActivityResult result = CropImage.getActivityResult(data);

                if (resultCode == RESULT_OK) {
                    Uri resultUri = result.getUri();

                    String fuser;
                    fuser = FirebaseAuth.getInstance().getCurrentUser().getUid();
                    reference = FirebaseDatabase.getInstance().getReference("Users").child(fuser);

                    StorageReference filepath=storageReference.child(fuser+".jpg");
                    image_profile.setImageURI(resultUri);

                    filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(Profile_beta.this, "Profile Image Updated", Toast.LENGTH_SHORT).show();
                            }

                        }
                    });

                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Exception error = result.getError();
                }

            }



        }
    }
}
java android firebase firebase-storage
1个回答
1
投票

您应该这样使用:

if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null ) {
        Uri ImageUri = data.getData();

        CropImage.activity(ImageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(1,1)
                .start(this);
}else if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                Uri resultUri = result.getUri();

                //.....

            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
               //...
            }
}
© www.soinside.com 2019 - 2024. All rights reserved.