我选择了要在屏幕上显示的图像,然后可以选择将其上传到firebase。但是,图像的大小为2-4mb,我想在不损失质量的情况下将其缩小。无论如何,我能做到吗?在将图像上传到Firebase之前,我希望将图像压缩到100kb或更小。
我已经尝试过使用该压缩器https://github.com/zetbaitsu/Compressor,但是在使其工作时遇到了麻烦。
这是我的完整代码:
public class AddNewItem extends AppCompatActivity {
private static final int CHOOSE_IMAGE = 177;
private ImageView imageViewAdd;
private EditText titleEditText, descriptionEditText, priceEditText, cityEditText;
private Button lanButton;
private String titleString, descriptionString, priceString, uploadID;
private Button buttonAdd, buttonRotateImage;
private ProgressBar progressBarImage;
private Uri uriSelectedImage;
private String downloadUriImage;
private FirebaseAuth mAuth;
private int imageRotation=0;
private AlertDialog.Builder mbuilder;
private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef, mDatabaseRefUser;
private StorageTask mUploadTask;
int x = -10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_item);
imageViewAdd = findViewById(R.id.select_image_id);
titleEditText = findViewById(R.id.title_edittext_id);
descriptionEditText = findViewById(R.id.description_edittext_id);
priceEditText = findViewById(R.id.price_edittext_id);
lanButton = findViewById(R.id.lan_button_id);
cityEditText = findViewById(R.id.city_textview_id);
buttonAdd = findViewById(R.id.add_item_button_id);
buttonRotateImage = findViewById(R.id.rotate_image_id);
progressBarImage = findViewById(R.id.progressbar_id);
mAuth = FirebaseAuth.getInstance();
mbuilder = new AlertDialog.Builder(this);
mStorageRef = FirebaseStorage.getInstance().getReference("uploads");
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRefUser = FirebaseDatabase.getInstance().getReference("Users");
}
public void selectImage(View view) {
Intent selectImageIntent = new Intent();
selectImageIntent.setType("image/*");
selectImageIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(selectImageIntent, "select image to show n upload"), CHOOSE_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uriSelectedImage = data.getData();
//new code
Picasso.with(this).load(uriSelectedImage).into(imageViewAdd);
//imageViewAdd.setScaleType(ImageView.ScaleType.FIT_XY); already in xml add_new_item
}
}
private String getFileExtension(Uri uri) {
ContentResolver cR = getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
return mime.getExtensionFromMimeType(cR.getType(uri));
}
//new code
public void uploadFile(View view) {
// the 1st if statement is to prevent spamming of upload button while upload is in progress
if (mUploadTask != null && mUploadTask.isInProgress()) {
Toast.makeText(this, "is in progress", Toast.LENGTH_SHORT).show();
return;
}
if (titleEditText.getText().toString().isEmpty() || titleEditText.getTextSize() < 5) {
titleEditText.setError("Title is short, minimum 5 characters");
titleEditText.requestFocus();
return;
}
if (descriptionEditText.getText().toString().isEmpty() || descriptionEditText.getTextSize() < 20) {
descriptionEditText.setError("Description is short, minimum 20 characters");
descriptionEditText.requestFocus();
return;
}
if(lanButton.getText().toString().isEmpty()){
lanButton.setError("Please Choose District");
lanButton.requestFocus();
return;
}
if (cityEditText.getText().toString().isEmpty() || cityEditText.length() < 3) {
cityEditText.setError("City/Location is short, minimum 3 characters");
cityEditText.requestFocus();
return;
}
if (priceEditText.getText().toString().isEmpty()) {
priceEditText.setError("Price is needed");
priceEditText.requestFocus();
return;
}
else {
if (uriSelectedImage != null) {
StorageReference fileReference = mStorageRef.child(System.currentTimeMillis() +
"." + getFileExtension(uriSelectedImage));
mUploadTask = fileReference.putFile(uriSelectedImage)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//Delays the progressbar with 0.5 secs before it shows 0
//user has 0.5 secs to see the 100% bar, code is not necenssary
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
progressBarImage.setProgress(0);
}
}, 500);
Toast.makeText(AddNewItem.this, "Upload succesful", Toast.LENGTH_SHORT).show();
//Upload image is a java class.
uploadID = mDatabaseRef.push().getKey();
UploadImage uploadImage = new UploadImage(titleEditText.getText().toString(),
taskSnapshot.getDownloadUrl().toString(), descriptionEditText.getText().toString(),
cityEditText.getText().toString(), priceEditText.getText().toString(),
imageRotation, lanButton.getText().toString(), mAuth.getCurrentUser().getUid(),
uploadID, mAuth.getCurrentUser().getEmail());
mDatabaseRef.child(uploadID).setValue(uploadImage);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(AddNewItem.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
progressBarImage.setProgress((int) progress);
}
});
} else {
Toast.makeText(this, "no file selected", Toast.LENGTH_SHORT).show();
}
}
}
public void rotateImage(View view) {
//set if condition if you want (for example if image has been set or so.... not necessary though.)
imageViewAdd.setRotation(imageViewAdd.getRotation() + 90);
imageRotation = (int) imageViewAdd.getRotation();
//imageViewAdd.setScaleType(ImageView.ScaleType.FIT_XY); already in xml add_new_item
}
@Override
protected void onStart() {
super.onStart();
if(mAuth.getCurrentUser() == null){
Intent mainActivityIntent = new Intent(this, MainActivity.class);
startActivity(mainActivityIntent);
finish();
}
else {
mDatabaseRefUser.child(mAuth.getCurrentUser().getUid()).child("Online").setValue(true);
}
}
}
清单中有<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
。
我该如何解决?
使用Compressor时,我将ACTION_GET_CONTENT
更改为ACTION_PICK
,然后添加了以下几行:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uriSelectedImage = data.getData();
File imageFile = new File(uriSelectedImage.getPath)
compressedImageBitmap = new Compressor(this).compressToBitmap(imageFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageByte = baos.toByteArray();
//new code
Picasso.with(this).load(uriSelectedImage).into(imageViewAdd);
//imageViewAdd.setScaleType(ImageView.ScaleType.FIT_XY); already in xml add_new_item
}
}
在onActivityResult
中
并同时声明private Bitmap compressedImageBitmap
private byte[] imageByte;
然后我将mUploadTask = fileReference.putFile(uriSelectedImage)
更改为putBytes(imageByte)
。但是,我在CompressedImageBitmap上收到一些错误,例如nullpointer。为什么会这样。
我该如何解决这个问题?