Reputation: 11
I'm a beginner on android programming. I have an app that saves image as byte array to sqlite db. in my android studio there is no error detected but when I execute insert (by a button), the app crash. I got the methods from a lot of tuts out there.
The image comes from the camera and show it in an ImageView.
I'm using Utility for the image and DataHandler for the db.
any idea what's wrong with my code? please describe it since I'm just a beginner :(
any help will be a great push for me.
here's my code
MyActivity.java
public class MyActivity extends Activity implements OnClickListener {
Button kirim;
EditText address, zipcode, otherID, stories, yearBuilt, screener, totalArea, buildingName, use;
DataHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
bn = (Button) findViewById(R.id.bn);
IMG = (ImageView) findViewById(R.id.img);
bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(i.resolveActivity(getPackageManager())!=null){
startActivityForResult(i, REQUEST_CODE);
}
}
});
kirim = (Button) findViewById(R.id.kirim);
address = (EditText) findViewById(R.id.editAddress);
zipcode = (EditText) findViewById(R.id.editZipCode);
otherID = (EditText) findViewById(R.id.editOtherIdentifier);
stories = (EditText) findViewById(R.id.editStories);
yearBuilt = (EditText) findViewById(R.id.editYearBuilt);
screener = (EditText) findViewById(R.id.editScreenerName);
totalArea = (EditText) findViewById(R.id.editTotalFloorArea);
buildingName = (EditText) findViewById(R.id.editBuildingName);
use = (EditText) findViewById(R.id.editUsage);
kirim.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
String getAddress = address.getText().toString();
String getZipcode = zipcode.getText().toString();
String getOtherID = otherID.getText().toString();
String getStories = stories.getText().toString();
String getYear = yearBuilt.getText().toString();
String getScreener = screener.getText().toString();
String getTotalarea = totalArea.getText().toString();
String getBuildingname = buildingName.getText().toString();
String getUse = use.getText().toString();
byte[] getImage = Utility.getBytes(BitmapFactory.decodeResource(getResources(), R.id.img));
handler = new DataHandler(getBaseContext());
handler.open();
long id = handler.insertData(getAddress, getZipcode, getOtherID, getStories, getYear, getScreener, getTotalarea, getBuildingname, getUse, getImage);
Toast.makeText(getBaseContext(), "Data Inserted", Toast.LENGTH_LONG).show();
}
});
Utility.java
public class Utility {
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getPhoto(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
DataHandler.java
public class DataHandler {
public static final String ADDRESS = "address";
public static final String ZIPCODE = "zipcode";
public static final String OTHERID = "otherID";
public static final String STORIES = "stories";
public static final String YEARBUILT = "yearBuilt";
public static final String SCREENER = "screener";
public static final String TOTALAREA = "totalArea";
public static final String BUILDINGNAME = "buildingName";
public static final String USAGE = "use";
public static final String IMAGE = "image";
public static final String TABLE_NAME = "mytable";
public static final String DATA_BASE_NAME = "mydb";
public static final int DATABASE_VERSION = 1;
DatabaseHelper dbhelper;
Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx){
this.ctx = ctx;
dbhelper = new DatabaseHelper(ctx);
}
private static class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context ctx){
super(ctx,DATA_BASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ADDRESS + " TEXT, " +
ZIPCODE + " TEXT NOT NULL, " +
OTHERID + " TEXT NOT NULL, " +
STORIES + " TEXT NOT NULL, " +
YEARBUILT + " TEXT, " +
IMAGE + " BLOB, " +
SCREENER + " TEXT, " +
TOTALAREA + " TEXT, " +
BUILDINGNAME + " TEXT, " +
USAGE + " TEXT, " +
IMAGE + " BLOB);"
);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
public DataHandler open(){
dbhelper = new DatabaseHelper(ctx);
db = dbhelper.getWritableDatabase();
return this;
}
public void close(){
dbhelper.close();
}
public long insertData(String address, String zipcode, String otherID, String stories, String yearBuilt, String screener, String totalArea, String buildingName, String use, byte[] image){
ContentValues content = new ContentValues();
content.put(ADDRESS, address);
content.put(ZIPCODE, zipcode);
content.put(OTHERID, otherID);
content.put(STORIES, stories);
content.put(YEARBUILT, yearBuilt);
content.put(SCREENER, screener);
content.put(TOTALAREA, totalArea);
content.put(BUILDINGNAME, buildingName);
content.put(USAGE, use);
content.put(IMAGE, image);
return db.insert(TABLE_NAME, null, content);
}}
here's the logcat shows error
8289-8289/com.example.brian.asprika E/OpenGLRenderer﹕ SFEffectCache:clear(), mSize = 0 01-09 14:03:33.733 8289-8289/com.example.brian.asprika W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection 01-09 14:03:43.193 8289-8289/com.example.brian.asprika I/HWUI﹕ EGLImpl-HWUI Protected EGL context created 01-09 14:03:46.593 8289-8289/com.example.brian.asprika D/AndroidRuntime﹕ Shutting down VM 01-09 14:03:46.593 8289-8289/com.example.brian.asprika W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417a3c08) 01-09 14:03:46.603 8289-8289/com.example.brian.asprika E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.brian.asprika, PID: 8289 java.lang.NullPointerException at com.example.brian.asprika.Utility.getBytes(Utility.java:15) at com.example.brian.asprika.MyActivity$2.onClick(MyActivity.java:89) at android.view.View.performClick(View.java:4637) at android.view.View$PerformClick.run(View.java:19422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5586) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 315
Reputation: 152817
byte[] getImage = Utility.getBytes(BitmapFactory.decodeResource(getResources(), R.id.img));
R.id.img
cannot be an image drawable identifier and the decodeResource()
returns null here.
Use a R.drawable
identifier for an image in your resources.
Upvotes: 1