· 9 years ago · Nov 22, 2016, 12:48 PM
1<?xml version="1.0" encoding="utf-8"?>
2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
4 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
5 android:paddingRight="@dimen/activity_horizontal_margin"
6 android:paddingTop="@dimen/activity_vertical_margin"
7 android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
8
9 <EditText
10 android:layout_width="match_parent"
11 android:layout_height="50dp"
12 android:textSize="24dp"
13 android:hint="enter name"
14 android:id="@+id/nametxt"
15 android:layout_marginTop="350dp"
16 />
17 <EditText
18 android:layout_width="match_parent"
19 android:layout_height="50dp"
20 android:textSize="24dp"
21 android:hint="enter id"
22 android:id="@+id/idtxt"
23 android:layout_marginTop="420dp"
24 />
25 <Button
26 android:textSize="20dp"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:text="Save Image Sqlite"
30 android:id="@+id/button"
31 android:layout_below="@+id/imageView"
32 android:layout_marginTop="75dp"
33 android:layout_alignParentLeft="true"
34 android:layout_alignParentStart="true"
35 android:layout_alignParentRight="true"
36 android:layout_alignParentEnd="true" />
37
38 <Button
39 android:textSize="20dp"
40
41 android:layout_width="match_parent"
42 android:layout_height="wrap_content"
43 android:text="Get Image From Sqlite"
44 android:id="@+id/button2"
45 android:layout_below="@+id/button"
46 android:layout_marginTop="42dp"
47 android:layout_alignParentRight="true"
48 android:layout_alignParentEnd="true" />
49
50 <ImageView
51 android:background="@color/colorPrimaryDark"
52 android:layout_width="120dp"
53 android:layout_height="120dp"
54 android:id="@+id/imageView2"
55 android:layout_alignParentTop="true"
56 android:layout_alignParentLeft="true"
57 android:layout_alignParentStart="true" />
58
59 <ImageView
60 android:background="@color/colorAccent"
61 android:layout_width="120dp"
62 android:layout_height="120dp"
63 android:id="@+id/imageView"
64 android:scaleType="fitCenter"
65 android:maxHeight="120dp"
66 android:maxWidth="120dp"
67 android:layout_alignParentTop="true"
68 android:layout_alignParentRight="true"
69 android:layout_alignParentEnd="true" />
70</RelativeLayout>
71
72package com.example.mahmoudbelal.navigator;
73
74import android.content.Intent;
75import android.database.Cursor;
76import android.database.sqlite.SQLiteDatabase;
77import android.graphics.Bitmap;
78import android.graphics.BitmapFactory;
79import android.net.Uri;
80import android.os.Bundle;
81import android.provider.MediaStore;
82import android.support.v7.app.AppCompatActivity;
83import android.view.View;
84import android.widget.Button;
85import android.widget.EditText;
86import android.widget.ImageView;
87import android.widget.Toast;
88
89import java.io.ByteArrayOutputStream;
90
91public class MainActivity extends AppCompatActivity {
92 private static int RESULT_LOAD_IMG = 1;
93 String imgDecodableString;
94static EditText name,id;
95
96
97
98 static ImageView Img,Img2;
99 Button Save,Get;
100 SQLiteDatabase db;
101
102 @Override
103 protected void onCreate(Bundle savedInstanceState) {
104 super.onCreate(savedInstanceState);
105 setContentView(R.layout.activity_main);
106id= (EditText) findViewById(R.id.idtxt);
107 Img = (ImageView) findViewById(R.id.imageView);
108 Img2 = (ImageView) findViewById(R.id.imageView2);
109 Save = (Button) findViewById(R.id.button);
110 Get = (Button) findViewById(R.id.button2);
111 name= (EditText) findViewById(R.id.nametxt);
112
113
114
115 Img.setOnClickListener(new View.OnClickListener() {
116 @Override
117 public void onClick(View view) {
118 Intent galleryIntent = new Intent(Intent.ACTION_PICK,
119 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
120 // Start the Intent
121 startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
122 }
123 });
124
125 Save.setOnClickListener(new View.OnClickListener() {
126 @Override
127 public void onClick(View v) {
128
129 try {
130
131
132 Img.buildDrawingCache();
133 Bitmap bitmap=Img.getDrawingCache();
134
135 ByteArrayOutputStream bos = new ByteArrayOutputStream();
136 bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
137 byte[] img = bos.toByteArray();
138
139 DBAdapter D=new DBAdapter();
140 D.open(MainActivity.this);
141 String myname=name.getText().toString();
142 String myid=id.getText().toString();
143
144 D.insert_user_data(myid,myname,img);
145
146 Toast.makeText(MainActivity.this, "Inserted Data well !", Toast.LENGTH_SHORT).show();
147
148 name.setText(" ");
149 id.setText(" ");
150
151 }catch (Exception e){
152 Toast.makeText(MainActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
153
154 }
155 }
156 });
157
158//------------------------------------------------------------------------------------//
159
160
161 Get.setOnClickListener(new View.OnClickListener() {
162 @Override
163 public void onClick(View v) {
164
165 DBAdapter D=new DBAdapter();
166 D.open(MainActivity.this);
167 String use_id=id.getText().toString();
168 D.Get_user_data(use_id);
169 if (DBAdapter.ok.equals("1")){
170 // Img2.setImageBitmap(DBAdapter.bmp);
171 // Toast.makeText(MainActivity.this, "name is = "+DBAdapter.s, Toast.LENGTH_SHORT).show();
172 name.setText(""+DBAdapter.s);
173
174 }else
175 {
176
177 Toast.makeText(MainActivity.this, "Error y m3lm ", Toast.LENGTH_SHORT).show();
178 } }
179 });
180 }
181
182
183 @Override
184 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
185 super.onActivityResult(requestCode, resultCode, data);
186
187
188
189 // When an Image is picked
190 if (requestCode == RESULT_LOAD_IMG && resultCode == this.RESULT_OK
191 && null != data) {
192 // Get the Image from data
193
194 Uri selectedImage = data.getData();
195 String[] filePathColumn = {MediaStore.Images.Media.DATA};
196
197 // Get the cursor
198 Cursor cursor = getContentResolver().query(selectedImage,
199 filePathColumn, null, null, null);
200
201 cursor.moveToFirst();
202
203 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
204 imgDecodableString = cursor.getString(columnIndex);
205
206//-0-------------------------------------------------------------------------------------------------------------
207 Uri photoUri = data.getData();
208 String[] proj = {MediaStore.Images.Media.DATA};
209
210 Cursor actualimagecursor = managedQuery(photoUri, proj, null, null, null);
211 int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
212 actualimagecursor.moveToFirst();
213
214 //--------------------------------------------------------------------------------------------
215 cursor.close();
216
217 Img.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
218
219 Toast.makeText(MainActivity.this, "Image Uploaded", Toast.LENGTH_LONG).show();
220
221
222 } else {
223 Toast.makeText(MainActivity.this, "You haven't picked Image", Toast.LENGTH_LONG).show();
224 }}
225
226
227
228
229
230 }
231
232package com.example.mahmoudbelal.navigator;
233
234import android.content.ContentValues;
235import android.content.Context;
236import android.database.Cursor;
237import android.database.sqlite.SQLiteDatabase;
238import android.database.sqlite.SQLiteOpenHelper;
239import android.graphics.Bitmap;
240import android.graphics.BitmapFactory;
241
242public class DBAdapter {
243 // first step:create table
244 static Bitmap bmp;
245 static String s="";
246 private int version=1;
247 private String DatabaseName="DNAA";
248 private String TableName="user";
249 static String ok;
250// -------------------------------------------------- //
251
252
253 private String create_table = "CREATE TABLE IF NOT EXISTS user(id varchar,name varchar,img blob);";
254
255
256
257 // second step:helper class
258 class DbHellper extends SQLiteOpenHelper{
259
260
261 public DbHellper(Context context){
262 super(context , DatabaseName , null , version);
263 }
264
265 @Override
266 public void onCreate(SQLiteDatabase db) {
267 db.execSQL(create_table);
268 }
269
270 @Override
271 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
272
273 db.execSQL("Drop table If Exists "+TableName);
274 onCreate(db);
275
276 }
277 }
278
279
280 private DbHellper DB_helper;
281 private SQLiteDatabase db;
282
283 public DBAdapter open(Context conn){
284 DB_helper = new DbHellper(conn);
285 db = DB_helper.getWritableDatabase();
286 return this;
287
288 // activity
289 }
290
291 private void close (){
292 db.close();
293 }
294 int i = 0;
295
296 public void insert_user_data (String id, String name, byte[] ii){
297 ContentValues cv = new ContentValues();
298
299
300 cv.put("id" , id);
301 cv.put("name" , name);
302 cv.put("img" , ii);
303
304 db.insert(TableName, null, cv);
305
306 }
307
308 public void updateSection(String username , String password , int rowId){
309 ContentValues cv = new ContentValues();
310 cv.put("username" , username);
311 cv.put("password" , password);
312 db.update(TableName, cv, "_id =" + rowId, null);
313 }
314
315 // Login method
316 public void Get_user_data (String id){
317
318 Cursor c = db.rawQuery("SELECT name,img FROM user where id='"+id+"'", null);
319 if (c.moveToFirst()) {
320 s= c.getString(0);
321 byte[] i=c.getBlob(1);
322
323 bmp= BitmapFactory.decodeByteArray(i,0,i.length);
324 MainActivity.Img2.setImageBitmap(bmp);
325
326 ok="1";
327 } else {
328 ok="0";
329 }
330 }
331 // delete data
332 public void deleteSection(int rowId){
333 db.delete(TableName, "_id =" + rowId, null);
334 }
335
336 public Cursor getAllSections (){
337 return db.query(TableName,new String []{"username","password"},null,null,null,null,null);
338 }
339
340 public Cursor getSection (String whereClause){
341 return db.query(TableName,new String []{"_id","name","devices"},whereClause,null,null,null,null);
342 }
343}