簡體   English   中英

如何使Uri圖像更小?

[英]How to make Uri image smaller?

我是Java的新手,我正在努力研究如何在將圖像上傳到Firebase之前調整其大小。

我已經閱讀並嘗試了很多建議,但沒有任何工作。

我只需要圖像為幾百KB。 該圖像將僅用於名稱(40dp * 40dp)旁邊的個人資料圖片。 因此質量不需要那么好。 因此,我並不是在尋找任何奇特的壓縮方式,而只是一種縮小圖像的簡單方法。 就像縮小寬度和高度一樣,只能說80dp *(長寬比)。

我從這樣的用戶電話獲取圖像:

private void openFileChooser() {

    Intent intent = new Intent();

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

}

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

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

         imageURI = data.getData();

        Picasso.with(this).load(imageURI).fit().centerCrop().into(imgView);

    }
}

並將其上傳到數據庫:

 private void uploadFile() {

    if (imageURI != null && getUserId != null) {

        final StorageReference ref = mStorage.child(getUserId);


        UploadTask uploadTask = ref.putFile(imageURI);
 }

我嘗試了這樣的擴展程序,但沒有幫助

 private void uploadFile() {

    if (imageURI != null && getUserId != null) {

        final StorageReference ref = mStorage.child(getUserId);

        Uri test = new Compressor() {
            public Uri compressToFile(Uri imageURI) {
                return imageURI;
            }

            @Override
            public String getMessageEncoding() {
                return null;
            }

            @Override
            public OutputStream compress(OutputStream os) throws IOException {
                return null;
            }
        }.compressToFile(imageURI);


        UploadTask uploadTask = ref.putFile(test);
} 

我發現的所有答案都沒有Uri的圖片,因此我有些困惑。 我可能需要將該圖像作為Firebase的Uri。 誰能教我一步一步的指導。 感謝所有幫助!

為此,您可以使用Imagecompressor庫https://github.com/zetbaitsu/Compressor

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM