· 6 years ago · Mar 09, 2019, 02:02 PM
1btnUpload.setOnClickListener(new View.OnClickListener() {
2 @Override
3 public void onClick(View v) {
4 ActivityCompat.requestPermissions(
5 SellerHome.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
6 REQUEST_CODE_GALLERY
7 );
8 }
9 });
10 btnStart.setOnClickListener(new View.OnClickListener() {
11 @Override
12 public void onClick(View v) {
13
14 String name = edtName.getText().toString();
15 String price = edtPrice.getText().toString();
16 String description = edtDescrip.getText().toString();
17 String duration = edtDuration.getText().toString();
18 byte[] imageView2 = ImageViewToByte(imageView);
19 myDB5.insertData(name,price,description,duration,imageView2);
20 Toast.makeText(getApplicationContext(),"Added Successfully",Toast.LENGTH_SHORT).show();
21 }
22 });
23}
24
25private byte[] ImageViewToByte(ImageView image) {
26 Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
27 ByteArrayOutputStream stream = new ByteArrayOutputStream();
28 bitmap.compress(Bitmap.CompressFormat.PNG,100,stream);
29 byte[] byteArray = stream.toByteArray();
30 return byteArray;
31}
32
33@Override
34public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
35 if(requestCode == REQUEST_CODE_GALLERY){
36 if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
37 Intent intent = new Intent(Intent.ACTION_PICK);
38 intent.setType("image/*");
39 startActivityForResult(intent,REQUEST_CODE_GALLERY);
40 }else{
41 Toast.makeText(getApplicationContext(),"You don't have permission to access file location!",Toast.LENGTH_SHORT).show();
42 }
43 return;
44 }
45
46 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
47}
48
49@Override
50protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
51 if(requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK && data != null){
52 Uri uri = data.getData();
53
54 try {InputStream inputStream = getContentResolver().openInputStream(uri);
55 Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
56 imageView.setImageBitmap(bitmap);
57 }
58
59 catch(FileNotFoundException e){
60 e.printStackTrace();
61 }
62
63 }
64
65 super.onActivityResult(requestCode, resultCode, data);
66}
67
68public void init(){
69 edtName = (EditText)findViewById(R.id.textName);
70 edtDescrip = (EditText)findViewById(R.id.textDescription);
71 edtPrice = (EditText)findViewById(R.id.textStartprice);
72 edtDuration = (EditText)findViewById(R.id.textDuration);
73 btnUpload = (Button)findViewById(R.id.buttonUpload);
74 btnStart = (Button)findViewById(R.id.buttonStart);
75 imageView = (ImageView)findViewById(R.id.imageView);
76
77}
78
79public class DatabaseHelper5 extends SQLiteOpenHelper {
80private final static String DBNAME = "Auction";
81private final static int DBVERSION = 2;
82
83SQLiteDatabase mDB5;
84
85public final static String TBL_AUCTION = "auction";
86public final static String COL_AUCTION_ID = BaseColumns._ID;
87public final static String COL_AUCTION_NAME = "name";
88public final static String COL_AUCTION_DESCRIPTION = "description";
89public final static String COL_AUCTION_PRICE = "price";
90public final static String COL_AUCTION_DURATION = "duration";
91public final static String COL_AUCTION_IMAGE = "image";
92
93
94private String crt_tbl_auction = "CREATE TABLE IF NOT EXISTS " + TBL_AUCTION + "(" +
95 COL_AUCTION_ID + " INTEGER PRIMARY KEY, " +
96 COL_AUCTION_NAME + " TEXT, " +
97 COL_AUCTION_DESCRIPTION + " TEXT, " +
98 COL_AUCTION_PRICE + " TEXT, " +
99 COL_AUCTION_DURATION + " TEXT, " +
100 COL_AUCTION_IMAGE + " BLOB " +
101 ")";
102
103public DatabaseHelper5(Context context) {
104 super(context, DBNAME, null, DBVERSION);
105 mDB5 = this.getWritableDatabase();
106}
107
108
109@Override
110public void onCreate(SQLiteDatabase db) {
111 db.execSQL(crt_tbl_auction);
112}
113
114@Override
115public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
116
117}
118
119public long insertData(String name, String description, String price, String duration, byte[] image) {
120 ContentValues cv = new ContentValues();
121 cv.put(COL_AUCTION_NAME, name);
122 cv.put(COL_AUCTION_DESCRIPTION, description);
123 cv.put(COL_AUCTION_PRICE, price);
124 cv.put(COL_AUCTION_DURATION, duration);
125 cv.put(COL_AUCTION_IMAGE, image);
126 return mDB5.insert(TBL_AUCTION, null, cv);
127}
128
129
130public Cursor queryData() {
131 SQLiteDatabase db = this.getReadableDatabase();
132
133 String[] projection = {
134 COL_AUCTION_NAME,
135 COL_AUCTION_DESCRIPTION,
136 COL_AUCTION_PRICE,
137 COL_AUCTION_DURATION,
138 COL_AUCTION_IMAGE
139 };
140
141 Cursor res = db.query(
142 TBL_AUCTION, // The table to query
143 projection, // The array of columns to return (pass null to get all)
144 null, null, null, null, BaseColumns._ID + " DESC", "1" // The sort order
145 );
146
147 return res;
148
149 //return mDB2.query(TBL_BID,null,null,null,null,null,COL_BID_NAME + " DESC", "1");
150}