I am a new firebase developer. I am developing an android app that lets users upload images to firebase storage. Because I am using Spark mode I only have 5Gb of storage. I want to limit the size of images that users upload to firebase storage. Below is the code that I use to upload an image to firebase storage.
Uri imageUri = Uri.parse(imagePath);
//need permistion android.permission.MANAGE_DOCUMENTS to upload file.
final StorageReference photoRef = mStorageRef.child(Constants.FIREBASE_LOCATION_IMAGE_EXAM).child(imageUri.getLastPathSegment());
taskStarted();
photoRef.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.d(TAG, "upload success");
mUploadExamTaskCount--;
examURLArray.add(new ExamPage(taskSnapshot.getDownloadUrl().toString()));
if (mUploadExamTaskCount == 0) {//upload files success.
/**
* update image link to database.
*/
DatabaseReference firebaseExamPage = FirebaseDatabase.getInstance().getReference(Constants.FIREBASE_LOCATION_EXAM_PAGE);
firebaseExamPage.child(examKey).setValue(examURLArray);
Log.d(TAG, "send upload exam success br");
Intent taskCompletedIntent = new Intent(ACTION_UPLOAD_EXAM_COMPLETED);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(taskCompletedIntent);
}
taskCompleted();
}
})
Do you have any idea how to resolve this problem ?
Update (20160519): Firebase just released a new feature called Firebase Storage. This allows you to upload images and other non-JSON data to a dedicated storage service. We highly recommend that you use this for storing images, instead of storing them as base64 encoded data in the JSON database.
Firebase provides up to 1GB of Storage for free on Firestore, the latest Google realtime database. After exhausting the free storage, users will pay for storage space and database operations.
Per the Firebase Storage Security Rules docs, you can write rules that check the size of an uploaded file:
service firebase.storage {
match /b/<bucket>/o {
match /files/{fileName} {
allow read;
allow write: if request.resource.size < 10 * 1024 * 1024; // 10MB limit for instance
}
}
}
If you goal is not necessarily to limit the user, but to keep the images in a low size, you could better resize the image on client side using JavaScript.
That was the perfect solution for us, saving:
If you are interested, you could follow this other answer from dcollien, in which we based the resizing:
https://stackoverflow.com/a/31669706/1920145
As one can see in that answer, the usage method is really simple, and the script to include is only one small JS / ES6 file.
I believe this guy (dcollien) deserves much more merit, so I suggest you to vote his answer and this answer up (so others can find it as relevant).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With