裁剪和压缩图像代码后无法将图像存储在 firebase 中

时间:2021-01-26 05:51:00

标签: java android firebase bitmap

我之前可以将图像上传到存储中,但是当我添加以下代码时,它现在无法存储。帮帮我。谢谢。

public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)  {super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
        imageuriOne = data.getData();

        CropImage.activity(imageuriOne)
                .setAspectRatio(1,1)
                .setMinCropWindowSize(400,400)
                .start(getContext(),this);
        if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if(resultCode == RESULT_OK) {
                Uri resultUri = result.getUri();

                File compressFilePath = new File(resultUri.getPath());

                if (compressFilePath.exists()) {
                    try {
                        Bitmap compressUri = new Compressor(this.getActivity()).setMaxWidth(800)
                                .setMaxHeight(600)
                                .setQuality(75)
                                .setCompressFormat(Bitmap.CompressFormat.WEBP)
                                .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
                                        Environment.DIRECTORY_PICTURES).getAbsolutePath())
                                .compressToBitmap(compressFilePath);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        compressUri.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                        byte[] compress_byte = baos.toByteArray();
                        //require api level26 (error), current is 23
                        String finalByte = Base64.getEncoder().encodeToString(compress_byte);
                        Uri finalUri = Uri.parse(finalByte);
                        dpOne.setImageURI(finalUri);
                        numap.put(1, "" + finalUri);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
        } earlier code...public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
        imageuriOne = data.getData();
        dpOne.setImageURI(imageuriOne);
        numap.put(1,""+imageuriOne);

    }
}

1 个答案:

答案 0 :(得分:0)

// 选择图像方法 /////

private void SelectImage() {


    // Defining Implicit Intent to mobile gallery
    Intent intent = new Intent();
    intent.setType("image/*");
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(

            Intent.createChooser(
                    galleryIntent,
                    "Select Image from here..."),
            PICK_IMAGE_REQUEST);
}

/////// 活动结果 /////////////

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

            // Get the Uri of data
            filePath = data.getData();
            try {

                // Setting image on image view using Bitmap
                Bitmap bitmap = MediaStore
                        .Images
                        .Media
                        .getBitmap(
                                getContentResolver(),
                                filePath);

                uploadImage(bitmap);

            } catch (IOException e) {
                // Log the exception
                e.printStackTrace();
            }
        }

//////////// 上传图片到 Firebase

 private void uploadImage(Bitmap bitmap) {

        // Code for showing progressDialog while uploading
        ProgressDialog progressDialog = new ProgressDialog(ChatActivity.this);
        progressDialog.setTitle("Uploading...");
        progressDialog.show();

        // Defining the child of storageReference
        media_IMG_Name = UUID.randomUUID().toString();
        StorageReference ref
                = storageReference.child("Media/" + current_login_user_id + "/" + media_IMG_Name + ".jpg");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] data = baos.toByteArray();

        final UploadTask uploadTask = ref.putBytes(data);
        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                progressDialog.dismiss();
                Toast.makeText(ChatActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();

                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();
                        }
                        return ref.getDownloadUrl();
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task) {
                        if (task.isSuccessful()) {
                            Uri downUri = task.getResult();
                           
                            Log.d("Final URL", "onComplete: Url: " + downUri.toString());
                        }
                    }
                });
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

                progressDialog.dismiss();
                Toast.makeText(ChatActivity.this, "Failed " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
相关问题