裁剪和压缩图像代码后无法将图像存储在Firebase中 [英] Not able to store image in firebase after cropping and compress image code

查看:51
本文介绍了裁剪和压缩图像代码后无法将图像存储在Firebase中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够早些时候将图像上传到存储中,但是当我添加以下代码时,它现在不存储了.帮帮我.谢谢.

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

    }
}

推荐答案

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

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

////////////将图像上传到Firebase

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

这篇关于裁剪和压缩图像代码后无法将图像存储在Firebase中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆