Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. private void insertData( String name,String pass, Uri image) throws SQLiteException {
  2. database = mdb.getWritableDatabase();
  3. ContentValues cv = new ContentValues();
  4. cv.put(MyDatabaseHelper.KEY_NAME, name);
  5. cv.put(MyDatabaseHelper.KEY_PASSWORD, pass);
  6. try {
  7. database = mdb.getWritableDatabase();
  8. InputStream iStream = getContentResolver().openInputStream(image);
  9. byte[] inputData = Utils.getBytes(iStream);
  10. cv.put(MyDatabaseHelper.KEY_IMAGE,inputData);
  11. }catch(IOException ioe)
  12. {
  13. Log.e(TAG, "<saveImageInDB> Error : " + ioe.getLocalizedMessage());
  14. }
  15. database.insert(MyDatabaseHelper.TABLE_USER, null, cv);
  16. Toast.makeText(getApplicationContext(),"Database Created",Toast.LENGTH_SHORT).show();
  17. database.close();
  18. }
  19.  
  20. public class MyDatabaseHelper extends SQLiteOpenHelper {
  21. public static final int DATABASE_VERSION=1;
  22. public static final String DATABASE_NAME="mm.db";
  23. public static final String TABLE_USER="User";
  24. public static final String KEY_NAME="Name";
  25. public static final String KEY_PASSWORD="Password";
  26. public static final String KEY_IMAGE="Image";
  27. public static final String ID="id";
  28.  
  29. public void onCreate(SQLiteDatabase db) {
  30. db.execSQL("create table " + TABLE_USER + " ( " + ID + " INTEGER PRIMARY KEY ,Name TEXT,Password TEXT,Image BLOB )");
  31. }
  32.  
  33. public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
  34. Log.w(MyDatabaseHelper.class.getName(), "Upgrading database from version" + oldVersion + "to" + newVersion + ",which will destroy all old data");
  35. db.execSQL("Drop TABLE IF EXISTS " + TABLE_USER);
  36. onCreate(db);
  37. }
  38.  
  39. public MyDatabaseHelper(Context context)
  40. {
  41. super(context, DATABASE_NAME,null,1);
  42. }
  43. }
  44.  
  45. public class Utils {
  46.  
  47. public static byte[] getImageBytes(Bitmap bitmap) {
  48. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  49. bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
  50. return stream.toByteArray();
  51. }
  52.  
  53. public static Bitmap getImage(byte[] image) {
  54. return BitmapFactory.decodeByteArray(image, 0, image.length);
  55. }
  56.  
  57. public static byte[] getBytes(InputStream inputStream) throws IOException {
  58. ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
  59. int bufferSize = 1024;
  60. byte[] buffer = new byte[bufferSize];
  61.  
  62. int len = 0;
  63. while ((len = inputStream.read(buffer)) != -1) {
  64. byteBuffer.write(buffer, 0, len);
  65. }
  66. return byteBuffer.toByteArray();
  67. }
  68. }
  69.  
  70. java.lang.NullPointerException
  71. at android.content.ContentResolver.openInputStream(ContentResolver.java:479)
  72. at com.example.seng.healthyapp.Register.insertData(Register.java:96)
  73. at com.example.seng.healthyapp.Register.access$000(Register.java:41)
  74. at com.example.seng.healthyapp.Register$1.onClick(Register.java:73)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement