firestore database add image to record

I need to add jpeg images to firestore db records. My preferred way would be in a Blob as I have used in Sqlite records. I have run into some problems in trying this. The following is my best try so far:

public Blob getImage() {
    Uri uri = Uri.fromFile(new 
File(mCurrentPhotoPath));
    File f1 = new File(mCurrentPhotoPath);
    URL myURL = null;
    try {

        myURL = f1.toURI().toURL();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    Bitmap bitmap = null;

    BitmapFactory.Options options = new 
BitmapFactory.Options();
    options.inPreferredConfig = 
Bitmap.Config.ARGB_8888;
    try {
        if (myURL != null) {
            bitmap = 
BitmapFactory.decodeStream( 
myURL.openConnection().getInputStream());
            String wBlob = 
encodeToBase64(bitmap,
 Bitmap.CompressFormat.JPEG, 100);
       blob = fromBase64String(wBlob);           
}


    } catch (java.io.IOException e) {
        e.printStackTrace();

    }
    return blob;
}

My problem is in the
blob = fromBase64String(wBlob);

I also have a problem with passing a blob to the class that sets the record. intent.putExtra("blobImage", blob ); I appreciate any help.

1 answer

  • answered 2018-03-13 22:21 Todd Kerpelman

    As a general rule, unless you're storing fairly small icons, I'd say, "Don't do this."

    While you can store native binary data in Cloud Firestore (you don't really need to base64-encode your image), you're going to run up against some of the limits in the database -- specifically, a single document can't be more than 1MB large.

    Instead, I would store the images themselves in Cloud Storage for Firebase and then just keep the download URLs in Cloud Firestore. Cloud Storage is approximately 7x cheaper in terms of storage costs, has much larger limits in terms of maximum file size, and by grabbing these download URLs, you can take advantage of third-party libraries like Picasso and Glide that can manage the image downloading for you, while also adding nifty features like memory or disk caching or showing placeholder graphics.

    You might want to check out this video for more information.