16

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 ?

joels
  • 7,249
  • 11
  • 53
  • 94
Zappy.Mans
  • 551
  • 1
  • 7
  • 19
  • You could limit the file size in firebase storage itself using the security rules. check out [this site](https://firebase.google.com/docs/storage/security/#data_validation) or the second part of [this video](https://www.youtube.com/watch?v=PUBnlbjZFAI&feature=youtu.be) – André Kool Jul 15 '16 at 17:06

3 Answers3

38

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
    }
  }
}
Mike McDonald
  • 15,609
  • 2
  • 46
  • 49
  • what happens, if i try to upload image with size more than 10Mb, will it compress or reject? – Anbarasu Chinna Jul 23 '18 at 08:17
  • It will be rejected. Compress client side if you want to have smaller images (or have a "drop zone" that has a higher size restriction and trigger cloud functions to resize images). – Mike McDonald Jul 24 '18 at 14:41
8

RESIZE ON CLIENT-SIDE USING JS

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:

  1. Space in our hosting,
  2. Money for that hosting space,
  3. Time of the visitor during upload (usually more than 10x less),
  4. Money of the visitor when uploading through mobile limited bandwith,
  5. Problems and bad experiences to the visitor when trying to upload a big photo that we would limit with other solutions

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).


2023 Update

The author of the previous recomended script is now recommending this other script: https://github.com/nodeca/pica

It can be tested here: http://nodeca.github.io/pica/demo/

Note: I haven't tested it in my codes yet, but should be better (the other one had a glitch).

DavidTaubmann
  • 3,223
  • 2
  • 34
  • 43
0

Might be late but can help another newbie :-) You can reduce the size of the upload using bytes this way:

StorageReference fileToUpload = mStorage.child("xyz").child(x);
                        ByteArrayOutputStream bao = new ByteArrayOutputStream();
                        photo.invalidate();
                        BitmapDrawable drawable = (BitmapDrawable) photo.getDrawable();
                        Bitmap  imp = drawable.getBitmap();
                        imp.compress(Bitmap.CompressFormat.JPEG, 25, bao);
                        byte[] byteArray = bao.toByteArray();
                        UploadTask uploadTask = fileToUpload.putBytes(byteArray);