Not able to store image in firebase after cropping and compress image code

时光总嘲笑我的痴心妄想 提交于 2021-01-29 08:37:11

问题


I was able to upload the image in storage earlier but when I added the following code it is not storing now. Help me out. Thanks.

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:


// Select Image method /////

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);
}

/////// On Activity Result /////////////

 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();
            }
        }

//////////// Upload Image to 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();
            }
        });
    }


来源:https://stackoverflow.com/questions/65896479/not-able-to-store-image-in-firebase-after-cropping-and-compress-image-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!