Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ran into some problems trying this.

This 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 line:

blob = fromBase64String(wBlob);

I also have a problem with passing a blob to the class that sets the record:

intent.putExtra("blobImage",  blob );
like image 999
rosey Avatar asked Mar 13 '18 21:03

rosey


People also ask

Can we store images in firestore database?

You can use our SDKs to store images, audio, video, or other user-generated content. On the server, you can use Google Cloud Storage APIs to access the same files.


Video Answer


1 Answers

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're going to run up against some limits in the database, specifically, a single document can't be more than 1 MB large. (You don't really need to Base64-encode your image.)

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 the "Getting Started with Firebase Storage on Android" video for more information.

like image 94
Todd Kerpelman Avatar answered Sep 20 '22 23:09

Todd Kerpelman