Created
June 14, 2012 23:27
-
-
Save vvakame/2933639 to your computer and use it in GitHub Desktop.
ORMLiteでBitmapなフィールドを良い感じに処理してくれるPersisterClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.vvakame.android.entity; | |
import java.io.ByteArrayOutputStream; | |
import java.sql.SQLException; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Bitmap.CompressFormat; | |
import com.j256.ormlite.field.FieldType; | |
import com.j256.ormlite.field.SqlType; | |
import com.j256.ormlite.field.types.BaseDataType; | |
import com.j256.ormlite.support.DatabaseResults; | |
public class BitmapType extends BaseDataType { | |
public BitmapType() { | |
super(SqlType.BYTE_ARRAY, new Class<?>[] { Bitmap.class }); | |
} | |
public static BitmapType getSingleton() { | |
return new BitmapType(); | |
} | |
@Override | |
public Object parseDefaultString(FieldType arg0, String arg1) | |
throws SQLException { | |
return null; | |
} | |
@Override | |
public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) | |
throws SQLException { | |
return super.sqlArgToJava(fieldType, sqlArg, columnPos); | |
} | |
@Override | |
public Object javaToSqlArg(FieldType fieldType, Object javaObject) | |
throws SQLException { | |
Bitmap bitmap = (Bitmap) javaObject; | |
ByteArrayOutputStream os = new ByteArrayOutputStream(); | |
bitmap.compress(CompressFormat.PNG, 100, os); | |
return os.toByteArray(); | |
} | |
@Override | |
public Object resultToJava(FieldType fieldType, DatabaseResults results, | |
int columnPos) throws SQLException { | |
byte[] bytes = results.getBytes(columnPos); | |
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); | |
} | |
@Override | |
public Object resultToSqlArg(FieldType arg0, DatabaseResults arg1, int arg2) | |
throws SQLException { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment