在上传到Firebase Android之前先压缩图像

时间:2019-01-13 12:57:33

标签: java android-studio

我正在尝试将图像压缩到Firebase存储中,但是当我添加putbytes()时,它要求我将代码转换为uploadtask。但是firebasestore的最新更新在uploadtask中没有getdownloaduri。请问如何将该代码添加到我的firebase存储代码中。

if(mImageUri!= null){

                final String randomName=UUID.randomUUID().toString();

//压缩图像开始

                File newImageFilepost = new File(mImageUri.getPath());
                try {

                    compressedImageFile = new Compressor(NewPostAnnonce.this)
                            .setMaxHeight(100)
                            .setMaxWidth(100)
                            .setQuality(5)
                            .compressToBitmap(newImageFilepost);

                } catch (IOException e) {
                    e.printStackTrace();
                }

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

//压缩图像结束                     最终的StorageReference image_path = storageReference.child(“ ImageProfile”)。child(randomName +“ .jpg”)。putBytes(imageData);

                image_path.putFile(mImageUri).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 image_path.getDownloadUrl();
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task) {
                        if (task.isSuccessful()){
                            Uri downUri = task.getResult();

                            storeFirestore(task, Lieu, Montant, Description, Lieuspinner, Salonspinner, Cuissinespinner,Salldebainspinner, Chambrespinner,Logementspinner);

                        }
                    }
                });


            }else {

                Toast.makeText(NewPostAnnonce.this, "Ajouter une photo", Toast.LENGTH_SHORT).show();

            }


        }
    });






    addImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){

                if(ContextCompat.checkSelfPermission(NewPostAnnonce.this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){

                    Toast.makeText(NewPostAnnonce.this, "Permission Denied", Toast.LENGTH_LONG).show();
                    ActivityCompat.requestPermissions(NewPostAnnonce.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

                } else {

                    BringImagePicker();

                }

            } else {

                BringImagePicker();

            }


        }
    });
}

private void storeFirestore(@NonNull Task<Uri> task,String Lieu,String Montant
        ,String Description,String Lieuspinner, String Salonspinner,String Cuissinespinner,
                            String Salldebainspinner,String Chambrespinner, String Logementspinner) {

    Uri download_uri;

    if (task !=null){

        download_uri=task.getResult();

    }else {

        download_uri=mImageUri;

    }


    Map<String, Object> postMap=new HashMap<>();
    postMap.put("image_url_1",download_uri.toString());
    postMap.put("LieuPreci",Lieu);
    postMap.put("Montant",Montant);
    postMap.put("Description",Description);
    postMap.put("Lieu",Lieuspinner);
    postMap.put("Salon",Salonspinner);
    postMap.put("Cuissine",Cuissinespinner);
    postMap.put("Salle de bain",Salldebainspinner);
    postMap.put("Chambre",Chambrespinner);
    postMap.put("Logement",Logementspinner);
    postMap.put("user_id", current_user_id);
    postMap.put("thumb",download_uri.toString());
    postMap.put("timestamp",FieldValue.serverTimestamp());
    postMap.put("Nom","Nom");
    postMap.put("Numero","Numero");
    postMap.put("ImageProfile","ImageProfile");


    firebaseFirestore.collection("UsersPost").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).set(postMap).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

            if (task.isSuccessful()){

                //   regProgress.setVisibility(View.INVISIBLE);


            }else {

                //   regProgress.setVisibility(View.INVISIBLE);

            }

        }
    });

}

0 个答案:

没有答案