Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void insertData( String name,String pass, Uri image) throws SQLiteException {
- database = mdb.getWritableDatabase();
- ContentValues cv = new ContentValues();
- cv.put(MyDatabaseHelper.KEY_NAME, name);
- cv.put(MyDatabaseHelper.KEY_PASSWORD, pass);
- try {
- database = mdb.getWritableDatabase();
- InputStream iStream = getContentResolver().openInputStream(image);
- byte[] inputData = Utils.getBytes(iStream);
- cv.put(MyDatabaseHelper.KEY_IMAGE,inputData);
- }catch(IOException ioe)
- {
- Log.e(TAG, "<saveImageInDB> Error : " + ioe.getLocalizedMessage());
- }
- database.insert(MyDatabaseHelper.TABLE_USER, null, cv);
- Toast.makeText(getApplicationContext(),"Database Created",Toast.LENGTH_SHORT).show();
- database.close();
- }
- public class MyDatabaseHelper extends SQLiteOpenHelper {
- public static final int DATABASE_VERSION=1;
- public static final String DATABASE_NAME="mm.db";
- public static final String TABLE_USER="User";
- public static final String KEY_NAME="Name";
- public static final String KEY_PASSWORD="Password";
- public static final String KEY_IMAGE="Image";
- public static final String ID="id";
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("create table " + TABLE_USER + " ( " + ID + " INTEGER PRIMARY KEY ,Name TEXT,Password TEXT,Image BLOB )");
- }
- public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
- Log.w(MyDatabaseHelper.class.getName(), "Upgrading database from version" + oldVersion + "to" + newVersion + ",which will destroy all old data");
- db.execSQL("Drop TABLE IF EXISTS " + TABLE_USER);
- onCreate(db);
- }
- public MyDatabaseHelper(Context context)
- {
- super(context, DATABASE_NAME,null,1);
- }
- }
- public class Utils {
- public static byte[] getImageBytes(Bitmap bitmap) {
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
- return stream.toByteArray();
- }
- public static Bitmap getImage(byte[] image) {
- return BitmapFactory.decodeByteArray(image, 0, image.length);
- }
- public static byte[] getBytes(InputStream inputStream) throws IOException {
- ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
- int bufferSize = 1024;
- byte[] buffer = new byte[bufferSize];
- int len = 0;
- while ((len = inputStream.read(buffer)) != -1) {
- byteBuffer.write(buffer, 0, len);
- }
- return byteBuffer.toByteArray();
- }
- }
- java.lang.NullPointerException
- at android.content.ContentResolver.openInputStream(ContentResolver.java:479)
- at com.example.seng.healthyapp.Register.insertData(Register.java:96)
- at com.example.seng.healthyapp.Register.access$000(Register.java:41)
- at com.example.seng.healthyapp.Register$1.onClick(Register.java:73)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement