'How to resize my image taken from internal storage before updating to Firebase cloud?
This is the logic where i take the picture from gallery/camera and I upload it to firebase cloud. I take other informations from another EditText and i checked if they i can upload or not.
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nume, pret,cantitate, categorie;
nume = etNume.getText().toString().trim();
pret = etPret.getText().toString().trim();
cantitate = etCantitate.getText().toString().trim();
categorie = etCategorie.getText().toString().trim();
if(TextUtils.isEmpty(nume)){
etNume.setError("Email is required.");
return;
}
if(TextUtils.isEmpty(pret)){
etPret.setError("Email is required.");
return;
}
if(TextUtils.isEmpty(cantitate)){
etCantitate.setError("Email is required.");
return;
}
if(TextUtils.isEmpty(categorie)){
etCategorie.setError("Email is required.");
return;
}
StorageReference file = mStorageRef.child("Produs").child(nume + ".jpg");
UploadTask uploadTask= file.putFile(mImage);
Task<Uri> urlTask = 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 file.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
Uri downloadUri =task.getResult();
path = downloadUri.toString();
Produs p=new Produs(nume,pret,cantitate,path,categorie);
Task task1 =mDatabase.child("Produs").child(nume).setValue(p);
task1.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Object o) {
Toast.makeText(Add.this, "Produsul a fost adaugat cu succes", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
});
task1.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Add.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
});
How can i upload my code to resize my image(taken from gallery/camera) to size 700X1000?
Solution 1:[1]
1 - First create a Bitmap from your URI/Path :
2 - After that, you can resize it (e.g. 700x1000):
private static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
}
3 - Save the resized bitmap to your device as a file and keep the path.
4 - Now upload it to the Firebase Cloud.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mouaad Abdelghafour AITALI |
