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