0
I want to resize the image that is in imageView coming from a camera or gallery before sending to Firebase, as you can see is being sent the "filepath" that is a Ri so I can not use scaleDown code. I am sending the image to the firebase Storage and the link goes to database. How can I resize before sending?
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == PICK_IMAGE_REQUEST || requestCode == REQUEST_IMAGE_CAPTURE) && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
if (requestCode == PICK_IMAGE_REQUEST) {
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_IMAGE_CAPTURE) {
filePath = data.getData();
Uri selectedImageUri = data.getData();
imageView.setImageURI(selectedImageUri);
}
}
}
>
StorageReference sRef = storageReference.child(Constants.STORAGE_PATH_UPLOADS + System.currentTimeMillis() + "." + getFileExtension(filePath));
//adding the file to reference
sRef.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//dismissing the progress dialog
progressDialog.dismiss();
//displaying success toast
Toast.makeText(getApplicationContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
//creating the upload object to store uploaded image details
Upload upload = new Upload(editTextName.getText().toString().trim(), editTextName1.getText().toString().trim(), editTextName2.getText().toString().trim(), taskSnapshot.getDownloadUrl().toString());
String uploadId = mDatabase.push().getKey();
mDatabase.child(uploadId).setValue(upload);
}
})
Scaleimage
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize, boolean filter) {
float ratio = Math.min(
(float) maxImageSize / realImage.getWidth(),
(float) maxImageSize / realImage.getHeight());
int width = Math.round((float) ratio * realImage.getWidth());
int height = Math.round((float) ratio * realImage.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,height, filter);
return newBitmap;
}
How would you implement this first code, put it inside onActivityResult? would change openFileDescriptor(Uri, "r"); to openFileDescriptor(filepath, "r");
– FranciscoM
Yes, it would be inside the onActivityResult or in a method the part, as you prefer. Would change openFileDescriptor(Uri, "r") to openFileDescriptor(selectedImageUri, "r"). I think the idea is to move the bitmap to the right firebase? Then you would pass the bitmap back to the storageReference
– André Ozawa
André, I’m having trouble implementing, what would be Roundedbitmapdrawableutil, is a method? , how to implement since I used two means, camera and gallery.
– FranciscoM
Roundedbitmapdrawableutil would be the class where the calculateInSampleSize method is in my project. In both cases in onActivityResult() the.getData() date returns you to Uri. And then just pass to the context.getContentResolver(). openFileDescriptor(Uri, "r"); (first line of the example)
– André Ozawa
Sorry André but I don’t understand, you have to edit my code and show how to implement your code? I appreciate your help.
– FranciscoM