Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 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. name = (EditText) findViewById(R.id.name);
  21.  
  22. name.addTextChangedListener(new TextWatcher() {
  23. @Override
  24. public void onTextChanged(CharSequence s, int start, int before, int count) {
  25. }
  26.  
  27. @Override
  28. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  29. }
  30.  
  31. @Override
  32. public void afterTextChanged(Editable editable) {
  33. String personName = name.getText().toString();
  34. database = mdb.getWritableDatabase();
  35. String selectQuery = " SELECT " + MyDatabaseHelper.KEY_IMAGE + " FROM " + MyDatabaseHelper.TABLE_USER + " WHERE " + MyDatabaseHelper.KEY_NAME + " = ' " + personName + " ' ";
  36. Cursor cursor = database.rawQuery(selectQuery, null);
  37. if (cursor.moveToFirst()) {
  38. byte[] blob = cursor.getBlob(cursor.getColumnIndex("Image"));
  39. Log.e("A", blob+"");
  40. cursor.close();
  41. mdb.close();
  42. imageView.setImageBitmap(getRoundedBitmap(Utils.getImage(blob))); // placed retrieved image into circular ImageView
  43. }
  44. }
  45. });
  46.  
  47. public Bitmap getRoundedBitmap(Bitmap bitmap){
  48. Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
  49. BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  50. Paint paint = new Paint();
  51. paint.setShader(shader);
  52. paint.setAntiAlias(true);
  53. Canvas c = new Canvas(circleBitmap);
  54. c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
  55. return circleBitmap;
  56. }
  57.  
  58. public class MyDatabaseHelper extends SQLiteOpenHelper {
  59. public static final int DATABASE_VERSION=1;
  60. public static final String DATABASE_NAME="mm.db";
  61. public static final String TABLE_USER="User";
  62. public static final String KEY_NAME="Name";
  63. public static final String KEY_PASSWORD="Password";
  64. public static final String KEY_IMAGE="Image";
  65. public static final String ID="id";
  66.  
  67. public void onCreate(SQLiteDatabase db) {
  68. db.execSQL("create table " + TABLE_USER + " ( " + ID + " INTEGER PRIMARY KEY ,Name TEXT,Password TEXT,Image BLOB )");
  69. }
  70.  
  71. public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
  72. Log.w(MyDatabaseHelper.class.getName(), "Upgrading database from version" + oldVersion + "to" + newVersion + ",which will destroy all old data");
  73. db.execSQL("Drop TABLE IF EXISTS " + TABLE_USER);
  74. onCreate(db);
  75. }
  76.  
  77. public MyDatabaseHelper(Context context)
  78. {
  79. super(context, DATABASE_NAME,null,1);
  80. }
  81. }
  82.  
  83. public class Utils {
  84.  
  85. public static byte[] getImageBytes(Bitmap bitmap) {
  86. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  87. bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
  88. return stream.toByteArray();
  89. }
  90.  
  91. public static Bitmap getImage(byte[] image) {
  92. return BitmapFactory.decodeByteArray(image, 0, image.length);
  93. }
  94.  
  95. public static byte[] getBytes(InputStream inputStream) throws IOException {
  96. ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
  97. int bufferSize = 1024;
  98. byte[] buffer = new byte[bufferSize];
  99.  
  100. int len = 0;
  101. while ((len = inputStream.read(buffer)) != -1) {
  102. byteBuffer.write(buffer, 0, len);
  103. }
  104. return byteBuffer.toByteArray();
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement