11package com .example .a390application ;
22
3+ import android .content .Intent ;
4+ import android .graphics .Bitmap ;
35import android .os .Bundle ;
6+ import android .provider .MediaStore ;
7+ import android .util .Base64 ;
48import android .view .LayoutInflater ;
59import android .view .View ;
610import android .view .ViewGroup ;
711import android .widget .AdapterView ;
812import android .widget .ArrayAdapter ;
913import android .widget .Button ;
1014import android .widget .EditText ;
15+ import android .widget .ImageView ;
1116import android .widget .Spinner ;
1217import android .widget .TextView ;
1318import android .widget .Toast ;
1924import com .example .a390application .InsertPlant .Plant ;
2025import com .google .firebase .database .DatabaseReference ;
2126import com .google .firebase .database .FirebaseDatabase ;
27+
28+ import java .io .ByteArrayOutputStream ;
2229import java .util .List ;
2330
2431
@@ -30,6 +37,9 @@ public class InsertPlantDialogFragment extends DialogFragment implements Adapter
3037 //private EditText PiIdEditText;
3138 private String typePicked ;
3239 protected String ownerID ;
40+ protected ImageView plantImage ;
41+
42+ static final int REQUEST_IMAGE_CAPTURE = 1 ;
3343
3444 @ Nullable
3545 @ Override
@@ -40,6 +50,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
4050 plantNameEditText = view .findViewById (R .id .plantNameEditText );
4151 //PiIdEditText = view.findViewById(R.id.PiIdEditText);
4252 Spinner plantTypeSpinner = view .findViewById (R .id .plantTypeDrop );
53+ plantImage = view .findViewById (R .id .plantImage );
4354
4455 ArrayAdapter <CharSequence > adapter = ArrayAdapter .createFromResource (getContext (), R .array .types , android .R .layout .simple_spinner_item );
4556 adapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
@@ -117,4 +128,39 @@ private void storePlantInDatabase(Plant givenPlant) {
117128 DatabaseReference userReference = FirebaseDatabase .getInstance ().getReference ().child ("Users" ).child (ownerID ).child (givenPlant .getName ());
118129 userReference .setValue (givenPlant );
119130 }
131+
132+ private void dispatchTakePictureIntent () {
133+ Intent takePictureIntent = new Intent (MediaStore .ACTION_IMAGE_CAPTURE );
134+ if (takePictureIntent .resolveActivity (getPackageManager ()) != null ) {
135+ startActivityForResult (takePictureIntent , REQUEST_IMAGE_CAPTURE );
136+ }
137+ }
138+
139+ @ Override
140+ protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
141+ super .onActivityResult (requestCode , resultCode , data );
142+ if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK ) {
143+ Bundle extras = data .getExtras ();
144+ Bitmap imageBitmap = (Bitmap ) extras .get ("data" );
145+ plantImage .setImageBitmap (imageBitmap );
146+ encodeBitmapAndSaveToFirebase (imageBitmap );
147+ new ImageSaver (this )
148+ .setFileName (givenPlant .getName () +".jpg" )
149+ .setExternal (false )//image save in external directory or app folder default value is false
150+ .setDirectory ("dir_name" )
151+ .save (imageBitmap ); //Bitmap from your code
152+ }
153+ }
154+
155+ public void encodeBitmapAndSaveToFirebase (Bitmap bitmap ) {
156+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
157+ bitmap .compress (Bitmap .CompressFormat .PNG , 100 , baos );
158+ String imageEncoded = Base64 .encodeToString (baos .toByteArray (), Base64 .DEFAULT );
159+ DatabaseReference ref = FirebaseDatabase .getInstance ()
160+ .getReference ().child ("Users" )
161+ .child (userID )
162+ .child (givenPlant .getName ())
163+ .child ("Image" );
164+ ref .setValue (imageEncoded );
165+ }
120166}
0 commit comments