Skip to content

Commit

Permalink
Annotate converters with @Nullable to prevent errors
Browse files Browse the repository at this point in the history
  • Loading branch information
udenr committed Jan 19, 2024
1 parent 927b910 commit 7f2115f
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.graphics.BitmapFactory;
import android.util.Base64;

import androidx.annotation.Nullable;
import androidx.room.TypeConverter;

import com.google.gson.Gson;
Expand All @@ -16,29 +17,31 @@
/**
* This class offers type converters for the room database.
*
* @see AppDatabase
*
* @author Christopher Beckmann
* @see AppDatabase
*/
public final class Converters {

@TypeConverter
@Nullable
public static String encodeImage(Bitmap bitmap) {
if(bitmap == null) return null;
if (bitmap == null) return null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.NO_WRAP);
}

@TypeConverter
@Nullable
public static Bitmap decodeImage(String encodedImage) {
if(encodedImage == null) return null;
if (encodedImage == null) return null;
byte[] decodedBytes = Base64.decode(encodedImage, Base64.NO_WRAP);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}

@TypeConverter
@Nullable
public static BarcodeFormat fromText(String text) {
try {
return BarcodeFormat.valueOf(text);
Expand All @@ -53,12 +56,14 @@ public static String fromBarcodeFormat(BarcodeFormat bcf) {
}

@TypeConverter
@Nullable
public static String fromResultPoints(ResultPoint[] rp) {
Gson gson = new Gson();
return gson.toJson(rp);
}

@TypeConverter
@Nullable
public static ResultPoint[] convertResultPointFromJson(String jsonString) {
Gson gson = new Gson();
return gson.fromJson(jsonString, ResultPoint[].class);
Expand Down

0 comments on commit 7f2115f

Please sign in to comment.