在将图像上传到Firebase存储之前压缩图像 [英] Compressing an Image before Uploading it to Firebase Storage

查看:330
本文介绍了在将图像上传到Firebase存储之前压缩图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在使用 SiliCompressor 库将其上传到 Firebase存储之前压缩图像,但似乎没有工作, ProgressDialog 不会停止。我第一次做的是通过单击ImageButton从图库中选择图像到ImageButton中。下面是我的代码。

  imageSelect.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType(image /);
startActivityForResult(galleryIntent,GALLERY_REQUEST);
}
});

------------------------------------------ ---------------------------

@Override
protected void onActivityResult(int requestCode,int resultCode ,意图数据){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == GALLERY_REQUEST&& resultCode == RESULT_OK){


imageUri = data.getData();

// imageSelect.setImageBitmap(BitmapFactory.decodeFile(imageSelectFile.getAbsolutePath()));
// Compressor com = Compressor.getDefault(this).compressToFile(imageFile);
// imageSelect.setImageURI(imageUri); (c).load(imageUri).fit()。(imageSelect);





$ p $所以现在我有一个方法 startPosting()其中上传数据通过点击按钮到Firebase存储。
以下是我的代码。

  private void startPosting(){
mProgress.setMessage(上传图片...);
//压缩图像....
String stringUri = imageUri.toString();
Uri uri_image_final;
// String filePath = SiliCompressor.with(getApplicationContext())。compress(stringUri);
String filePath = SiliCompressor.with(getApplicationContext())。compress(stringUri,true);
uri_image_final = Uri.parse(filePath);
System.out.println(Whats here:+
+ uri_image_final);

final String title_val = mPostTitle.getText()。toString()。trim();
final String desc_val = mPostDesc.getText()。toString()。trim();
if(!TextUtils.isEmpty(title_val)&!TextUtils.isEmpty(desc_val)&& filePath!= null){
mProgress.show();
StorageReference filepath = mStorage.child(BlogImages)。child(uri_image_final.getLastPathSegment());
filepath.putFile(uri_image_final).addOnSuccessListener(new OnSuccessListener< UploadTask.TaskSnapshot>(){
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot){


Uri downloadUri = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
DatabaseReference c = mDatabase.push();
newPost.child(EventTitle ).setValue(title_val);
newPost.child(EventDescription)。setValue(desc_val);
newPost.child(EventImage)。setValue(downloadUri.toString());
newPost.child(PostId)。setValue(c);


$ b mProgress.dismiss();
startActivity(new Intent(PostActivity.this ,MainActivity.class));


}
}


);

$ b} else if(TextUtils.isEmpty(title_val)&&& TextUtils.isEmpty(desc_val)&& imageUri!= null){
mProgress.show );
StorageReference filepath = mStorage.child(BlogImages)。child(uri_image_final.getLastPathSegment());
filepath.putFile(uri_image_final).addOnSuccessListener(new OnSuccessListener< UploadTask.TaskSnapshot>(){
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot){


Uri downloadUri = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child(EventTitle)。setValue();
(EventDescription)。setValue();
newPost.child(EventImage)。setValue(downloadUri.toString());


mProgress .dismiss();
// startActivity(new Intent(PostActivity.this,MainActivity.class));
Intent load = new Intent(PostActivity.this,MainActivity.class);
load .putExtra(eventname,eventname);
startActivity(load);


}
}


); (!TextUtils.isEmpty(title_val)&&!TextUtils.isEmpty(desc_val)& imageUri == null){




$ b Toast.makeText(getApplicationContext(),请插入图片并上传!,Toast.LENGTH_LONG).show();





$ p $现在如果你看到那个方法,我的本质是压缩ImageButton中加载的图像,然后将其上传到firebase。
这是 Silicon Compressor 的行,它试图压缩在ImageButton中加载的图像。

  String filePath = SiliCompressor.with(getApplicationContext())。compress(stringUri,true); 

我从Github的这个链接中得到了这个lib。
https://github.com/Tourenathan-G5organisation/SiliCompressor



所以我错在哪里,因为图片不是上传,但我希望它在压缩时上传。



  @Override $ b 

解决方案

这是我写的内容,可以试试。 b保护void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
$ b $ if(resultCode == RESULT_OK){
if(requestCode == Constants.PICK_USER_PROFILE_IMAGE){
if(resultCode == RESULT_OK){
Bitmap bmp = ImagePicker .getImageFromResult(this,resultCode,data); //你的压缩位图
startPosting(bmp);



$ b code
$ b

你的startPosting方法应该是这样的

  private void startPosting(Bitmap bmp){
byte [] data = bmp.toByteArray ();
mProgress.setMessage(正在上传图片...);

final String title_val = mPostTitle.getText()。toString()。trim();
final String desc_val = mPostDesc.getText()。toString()。trim();
if(!TextUtils.isEmpty(title_val)&!TextUtils.isEmpty(desc_val)&& filePath!= null){
mProgress.show();
StorageReference filepath = mStorage.child(BlogImages)。child(uri_image_final.getLastPathSegment());


UploadTask uploadTask = filepath.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener(){
@Override $ b $ public void onFailure(@NonNull异常异常){
//处理不成功的上传
}
)}。addOnSuccessListener(new OnSuccessListener< UploadTask.TaskSnapshot>(){
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot){

Uri downloadUri = taskSnapshot.getDownloadUrl ();
DatabaseReference newPost = mDatabase.push();
DatabaseReference c = mDatabase.push();
newPost.child(EventTitle).setValue(title_val); $ b $ (EventDescription)。setValue(desc_val);
newPost.child(EventImage)。setValue(downloadUri.toString());
newPost.child(PostId)。 setValue(c);



mProgress.dismiss();
startActivity(n ew Intent(PostActivity.this,MainActivity.class));


}
});

$ b} else if(TextUtils.isEmpty(title_val)&&& TextUtils.isEmpty(desc_val)&& imageUri!= null){
mProgress.show );
StorageReference filepath = mStorage.child(BlogImages)。child(uri_image_final.getLastPathSegment());
UploadTask uploadTask = filepath.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener(){
@Override $ b $ public void onFailure(@NonNull异常异常){
//处理不成功的上传
}
)}。addOnSuccessListener(new OnSuccessListener< UploadTask.TaskSnapshot>(){
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot){

Uri downloadUri = taskSnapshot.getDownloadUrl ();
DatabaseReference newPost = mDatabase.push();
newPost.child(EventTitle)。setValue();
newPost.child(EventDescription)。setValue );
newPost.child(EventImage)。setValue(downloadUri.toString());


mProgress.dismiss();
// startActivity(new Intent(PostActivity.this,MainActivity.class));
意图加载= new Intent(Pos tActivity.this,MainActivity.class);
load.putExtra(eventname,eventname);
startActivity(load);


}
}); (!TextUtils.isEmpty(title_val)&&!TextUtils.isEmpty(desc_val)& imageUri == null){




$ b Toast.makeText(getApplicationContext(),请插入图片并上传!,Toast.LENGTH_LONG).show();


$ b $ / code $ / pre
$ b $ p下面是ImagePicker类一系列的方法来完成你的工作

pre $ c $ public $ Image public class ImagePicker {

private static final int DEFAULT_MIN_WIDTH_QUALITY = 400 ; //最小像素
private static final String TAG =ImagePicker;
private static final String TEMP_IMAGE_NAME =tempImage;

public static int minWidthQuality = DEFAULT_MIN_WIDTH_QUALITY;

public static Bitmap getImageFromResult(Context context,int resultCode,
Intent imageReturnedIntent){
Log.d(TAG,getImageFromResult,resultCode:+ resultCode);
位图bm = null;
文件imageFile = getTempFile(context);
if(resultCode == Activity.RESULT_OK){
Uri selectedImage;
boolean isCamera =(imageReturnedIntent == null ||
imageReturnedIntent.getData()== null ||
imageReturnedIntent.getData()。equals(Uri.fromFile(imageFile)));
if(isCamera){/ ** CAMERA ** /
selectedImage = Uri.fromFile(imageFile);
} else {/ ** ALBUM ** /
selectedImage = imageReturnedIntent.getData();
}
Log.d(TAG,selectedImage:+ selectedImage);

bm = getImageResized(context,selectedImage);
int rotation = getRotation(context,selectedImage,isCamera);
bm = rotate(bm,rotation);
}
return bm;


$ b私有静态位图decodeBitmap(上下文上下文,Uri theUri,int sampleSize){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sampleSize;

AssetFileDescriptor fileDescriptor = null;
try {
fileDescriptor = context.getContentResolver()。openAssetFileDescriptor(theUri,r);
} catch(FileNotFoundException e){
e.printStackTrace();


位图actualUsableBitmap = BitmapFactory.decodeFileDescriptor(
fileDescriptor.getFileDescriptor(),null,options);

Log.d(TAG,options.inSampleSize +sample method bitmap ...+
actualUsableBitmap.getWidth()++ actualUsableBitmap.getHeight());

返回actualUsableBitmap;

$ b / **
*调整大小以避免使用太大的内存加载大图像(例如:2560 * 1920)
** /
private静态位图getImageResized(上下文上下文,Uri selectedImage){
位图bm = null;
int [] sampleSizes = new int [] {5,3,2,1};
int i = 0;
do {
bm = decodeBitmap(context,selectedImage,sampleSizes [i]);
Log.d(TAG,resizer:new bitmap width =+ bm.getWidth());
i ++;
} while(bm.getWidth()< minWidthQuality&& i< sampleSizes.length);
return bm;



private static int getRotation(上下文上下文,Uri imageUri,boolean isCamera){
int rotation;
if(isCamera){
rotation = getRotationFromCamera(context,imageUri);
} else {
rotation = getRotationFromGallery(context,imageUri);
}
Log.d(TAG,Image rotation:+ rotation);
回报轮换;


private static int getRotationFromCamera(上下文上下文,Uri imageFile){
int rotate = 0;
尝试{

context.getContentResolver()。notifyChange(imageFile,null);
ExifInterface exif = new ExifInterface(imageFile.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);

switch(orientation){
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch(Exception e){
e.printStackTrace();
}
return rotate;


public static int getRotationFromGallery(Context context,Uri imageUri){
String [] columns = {MediaStore.Images.Media.ORIENTATION};
Cursor cursor = context.getContentResolver()。query(imageUri,columns,null,null,null);
if(cursor == null)return 0;

cursor.moveToFirst();

int orientationColumnIndex = cursor.getColumnIndex(columns [0]);
返回cursor.getInt(orientationColumnIndex);



private static Bitmap rotate(Bitmap bm,int rotation){
if(rotation!= 0){
矩阵矩阵=新矩阵();
matrix.postRotate(rotation);
位图bmOut = Bitmap.createBitmap(bm,0,bm.getWidth(),bm.getHeight(),matrix,true);
return bmOut;
}
return bm;


$ b private static File getTempFile(Context context){
File imageFile = new File(context.getExternalCacheDir(),TEMP_IMAGE_NAME);
imageFile.getParentFile()。mkdirs();
返回imageFile;




$ b ImagePicker类具有所有处理压缩的方法,以及图像的旋转。

希望它会有所帮助

感谢上传文件ref的链接

p>

上传firebase上的文件


I am trying to Compress an Image before Uploading it to Firebase Storage using SiliCompressor library , but it seems not working , the ProgressDialog doesn't stop. What i first did was to pick the Image from the Gallery into an ImageButton by clicking an ImageButton. Below is my code.

imageSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/");
                startActivityForResult(galleryIntent, GALLERY_REQUEST);
            }
        });

---------------------------------------------------------------------

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK) {


            imageUri = data.getData();

           // imageSelect.setImageBitmap(BitmapFactory.decodeFile(imageSelectFile.getAbsolutePath()));
        //    Compressor com = Compressor.getDefault(this).compressToFile(imageFile);
           // imageSelect.setImageURI(imageUri);
            Picasso.with(c).load(imageUri).fit().into(imageSelect);

        }
    }

So now am having a Method startPosting() which Uploads data By a click of Button to Firebase Storage. Below is my code.

 private void startPosting() {
        mProgress.setMessage("Uploading Image...");
        //Compressing an Image ....
        String stringUri= imageUri.toString();
        Uri uri_image_final;
        //String filePath = SiliCompressor.with(getApplicationContext()).compress(stringUri);
        String filePath = SiliCompressor.with(getApplicationContext()).compress(stringUri, true);
        uri_image_final = Uri.parse(filePath);
        System.out.println("Whats here :" +
                ""+ uri_image_final);

        final String title_val = mPostTitle.getText().toString().trim();
        final String desc_val = mPostDesc.getText().toString().trim();
        if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && filePath != null) {
            mProgress.show();
            StorageReference filepath = mStorage.child("BlogImages").child(uri_image_final.getLastPathSegment());
            filepath.putFile(uri_image_final).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


                    Uri downloadUri = taskSnapshot.getDownloadUrl();
                    DatabaseReference newPost = mDatabase.push();
                    DatabaseReference c = mDatabase.push();
                    newPost.child("EventTitle").setValue(title_val);
                    newPost.child("EventDescription").setValue(desc_val);
                    newPost.child("EventImage").setValue(downloadUri.toString());
                    newPost.child("PostId").setValue(c);



                    mProgress.dismiss();
                    startActivity(new Intent(PostActivity.this, MainActivity.class));


                }
            }


            );


        } else if (TextUtils.isEmpty(title_val) && TextUtils.isEmpty(desc_val) && imageUri != null) {
            mProgress.show();
            StorageReference filepath = mStorage.child("BlogImages").child(uri_image_final.getLastPathSegment());
            filepath.putFile(uri_image_final).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


                    Uri downloadUri = taskSnapshot.getDownloadUrl();
                    DatabaseReference newPost = mDatabase.push();
                    newPost.child("EventTitle").setValue("");
                    newPost.child("EventDescription").setValue("");
                    newPost.child("EventImage").setValue(downloadUri.toString());


                    mProgress.dismiss();
                   // startActivity(new Intent(PostActivity.this, MainActivity.class));
                  Intent load=  new Intent(PostActivity.this,MainActivity.class);
                    load.putExtra(eventname,eventname);
                    startActivity(load);


                }
            }


            );

        }

        else if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && imageUri== null){

            Toast.makeText(getApplicationContext(),"Please insert an Image and Upload ! ",Toast.LENGTH_LONG).show();

        }
    }

Now if you see in that Method , my essence was to Compress an Image which is loaded in the ImageButton, then Afterwards it Uploads to firebase. This is the line of Silicon Compressor which tries to compress an image loaded in the ImageButton.

 String filePath = SiliCompressor.with(getApplicationContext()).compress(stringUri, true);

I got this lib from this link of Github. https://github.com/Tourenathan-G5organisation/SiliCompressor

So where am i wrong please because the Image is not Uploading but i want it to Upload while it's compressed.

解决方案

Here is what I have written, you can try it.

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

    if (resultCode == RESULT_OK) {
        if (requestCode == Constants.PICK_USER_PROFILE_IMAGE) {
            if (resultCode == RESULT_OK) {
                Bitmap bmp = ImagePicker.getImageFromResult(this, resultCode, data);//your compressed bitmap here
                startPosting(bmp);
            }
        }
    }
}

Your startPosting method should be like this

 private void startPosting(Bitmap bmp) {
    byte[] data = bmp.toByteArray();
    mProgress.setMessage("Uploading Image...");

    final String title_val = mPostTitle.getText().toString().trim();
    final String desc_val = mPostDesc.getText().toString().trim();
    if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && filePath != null) {
        mProgress.show();
        StorageReference filepath = mStorage.child("BlogImages").child(uri_image_final.getLastPathSegment());


        UploadTask uploadTask = filepath.putBytes(data);
        uploadTask.addOnFailureListener(new OnFailureListener() {
              @Override
              public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
              }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
              @Override
              public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            Uri downloadUri = taskSnapshot.getDownloadUrl();
            DatabaseReference newPost = mDatabase.push();
            DatabaseReference c = mDatabase.push();
            newPost.child("EventTitle").setValue(title_val);
            newPost.child("EventDescription").setValue(desc_val);
            newPost.child("EventImage").setValue(downloadUri.toString());
            newPost.child("PostId").setValue(c);



            mProgress.dismiss();
            startActivity(new Intent(PostActivity.this, MainActivity.class));


        }
    });


    } else if (TextUtils.isEmpty(title_val) && TextUtils.isEmpty(desc_val) && imageUri != null) {
        mProgress.show();
        StorageReference filepath = mStorage.child("BlogImages").child(uri_image_final.getLastPathSegment());
        UploadTask uploadTask = filepath.putBytes(data);
        uploadTask.addOnFailureListener(new OnFailureListener() {
              @Override
              public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
              }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
              @Override
              public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Uri downloadUri = taskSnapshot.getDownloadUrl();
                DatabaseReference newPost = mDatabase.push();
                newPost.child("EventTitle").setValue("");
                newPost.child("EventDescription").setValue("");
                newPost.child("EventImage").setValue(downloadUri.toString());


                mProgress.dismiss();
               // startActivity(new Intent(PostActivity.this, MainActivity.class));
                Intent load=  new Intent(PostActivity.this,MainActivity.class);
                load.putExtra(eventname,eventname);
                startActivity(load);


            }
        });

    }

    else if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && imageUri== null){

        Toast.makeText(getApplicationContext(),"Please insert an Image and Upload ! ",Toast.LENGTH_LONG).show();

    }
}

Below is the class ImagePicker that have series of methods to do your work

public class ImagePicker {

private static final int DEFAULT_MIN_WIDTH_QUALITY = 400;        // min pixels
private static final String TAG = "ImagePicker";
private static final String TEMP_IMAGE_NAME = "tempImage";

public static int minWidthQuality = DEFAULT_MIN_WIDTH_QUALITY;

public static Bitmap getImageFromResult(Context context, int resultCode,
                                        Intent imageReturnedIntent) {
    Log.d(TAG, "getImageFromResult, resultCode: " + resultCode);
    Bitmap bm = null;
    File imageFile = getTempFile(context);
    if (resultCode == Activity.RESULT_OK) {
        Uri selectedImage;
        boolean isCamera = (imageReturnedIntent == null ||
                imageReturnedIntent.getData() == null ||
                imageReturnedIntent.getData().equals(Uri.fromFile(imageFile)));
        if (isCamera) {     /** CAMERA **/
            selectedImage = Uri.fromFile(imageFile);
        } else {            /** ALBUM **/
            selectedImage = imageReturnedIntent.getData();
        }
        Log.d(TAG, "selectedImage: " + selectedImage);

        bm = getImageResized(context, selectedImage);
        int rotation = getRotation(context, selectedImage, isCamera);
        bm = rotate(bm, rotation);
    }
    return bm;
}


private static Bitmap decodeBitmap(Context context, Uri theUri, int sampleSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize;

    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    Bitmap actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor(
            fileDescriptor.getFileDescriptor(), null, options);

    Log.d(TAG, options.inSampleSize + " sample method bitmap ... " +
            actuallyUsableBitmap.getWidth() + " " + actuallyUsableBitmap.getHeight());

    return actuallyUsableBitmap;
}

/**
 * Resize to avoid using too much memory loading big images (e.g.: 2560*1920)
 **/
private static Bitmap getImageResized(Context context, Uri selectedImage) {
    Bitmap bm = null;
    int[] sampleSizes = new int[]{5, 3, 2, 1};
    int i = 0;
    do {
        bm = decodeBitmap(context, selectedImage, sampleSizes[i]);
        Log.d(TAG, "resizer: new bitmap width = " + bm.getWidth());
        i++;
    } while (bm.getWidth() < minWidthQuality && i < sampleSizes.length);
    return bm;
}


private static int getRotation(Context context, Uri imageUri, boolean isCamera) {
    int rotation;
    if (isCamera) {
        rotation = getRotationFromCamera(context, imageUri);
    } else {
        rotation = getRotationFromGallery(context, imageUri);
    }
    Log.d(TAG, "Image rotation: " + rotation);
    return rotation;
}

private static int getRotationFromCamera(Context context, Uri imageFile) {
    int rotate = 0;
    try {

        context.getContentResolver().notifyChange(imageFile, null);
        ExifInterface exif = new ExifInterface(imageFile.getPath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}

public static int getRotationFromGallery(Context context, Uri imageUri) {
    String[] columns = {MediaStore.Images.Media.ORIENTATION};
    Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null);
    if (cursor == null) return 0;

    cursor.moveToFirst();

    int orientationColumnIndex = cursor.getColumnIndex(columns[0]);
    return cursor.getInt(orientationColumnIndex);
}


private static Bitmap rotate(Bitmap bm, int rotation) {
    if (rotation != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(rotation);
        Bitmap bmOut = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        return bmOut;
    }
    return bm;
}


 private static File getTempFile(Context context) {
        File imageFile = new File(context.getExternalCacheDir(), TEMP_IMAGE_NAME);
        imageFile.getParentFile().mkdirs();
        return imageFile;
    }
}

The ImagePicker Class have all the methods of handling compression as well as rotation of image.

Hope it will help

Thanks to this link for uploading file ref

Uploading files on firebase

这篇关于在将图像上传到Firebase存储之前压缩图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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