Skip to content

Commit 45ee3e5

Browse files
committed
Merge remote-tracking branch 'origin/ApplicationUI-to-Firebase' into ApplicationUI-to-Firebase
2 parents 9771ef7 + efea269 commit 45ee3e5

File tree

4 files changed

+62
-53
lines changed

4 files changed

+62
-53
lines changed

390application/app/src/main/java/com/example/a390application/InsertPlantDialogFragment.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.example.a390application;
22

3+
import android.content.Intent;
4+
import android.graphics.Bitmap;
35
import android.os.Bundle;
6+
import android.provider.MediaStore;
7+
import android.util.Base64;
48
import android.view.LayoutInflater;
59
import android.view.View;
610
import android.view.ViewGroup;
711
import android.widget.AdapterView;
812
import android.widget.ArrayAdapter;
913
import android.widget.Button;
1014
import android.widget.EditText;
15+
import android.widget.ImageView;
1116
import android.widget.Spinner;
1217
import android.widget.TextView;
1318
import android.widget.Toast;
@@ -19,6 +24,8 @@
1924
import com.example.a390application.InsertPlant.Plant;
2025
import com.google.firebase.database.DatabaseReference;
2126
import com.google.firebase.database.FirebaseDatabase;
27+
28+
import java.io.ByteArrayOutputStream;
2229
import 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
}

390application/app/src/main/java/com/example/a390application/inspectPlantActivity.java

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class inspectPlantActivity extends AppCompatActivity {
3434
protected TextView plantsType;
3535
protected TextView plantsInfo;
3636
protected TextView plantsInfo2;
37-
protected ImageView plantImage;
37+
3838
protected FloatingActionButton takeImageFloatingButton;
3939
protected Button deletePlantButton;
4040
protected Bundle bundle = new Bundle();
@@ -73,7 +73,7 @@ protected void onCreate(Bundle savedInstanceState) {
7373
plantsType = findViewById(R.id.plantType);
7474
plantsInfo = findViewById(R.id.plantInfo);
7575
plantsInfo2 = findViewById(R.id.plantInfo2);
76-
plantImage = findViewById(R.id.plantImage);
76+
7777

7878

7979

@@ -306,41 +306,4 @@ public String fetchIdealTemperature(String type) {
306306
}
307307
return idealTemperature;
308308
}
309-
static final int REQUEST_IMAGE_CAPTURE = 1;
310-
311-
private void dispatchTakePictureIntent() {
312-
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
313-
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
314-
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
315-
}
316-
}
317-
318-
@Override
319-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
320-
super.onActivityResult(requestCode, resultCode, data);
321-
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
322-
Bundle extras = data.getExtras();
323-
Bitmap imageBitmap = (Bitmap) extras.get("data");
324-
plantImage.setImageBitmap(imageBitmap);
325-
encodeBitmapAndSaveToFirebase(imageBitmap);
326-
new ImageSaver(this)
327-
.setFileName(givenPlant.getName() +".jpg")
328-
.setExternal(false)//image save in external directory or app folder default value is false
329-
.setDirectory("dir_name")
330-
.save(imageBitmap); //Bitmap from your code
331-
}
332-
}
333-
334-
public void encodeBitmapAndSaveToFirebase(Bitmap bitmap) {
335-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
336-
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
337-
String imageEncoded = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
338-
DatabaseReference ref = FirebaseDatabase.getInstance()
339-
.getReference().child("Users")
340-
.child(userID)
341-
.child(givenPlant.getName())
342-
.child("Image");
343-
ref.setValue(imageEncoded);
344-
}
345-
346309
}

390application/app/src/main/res/layout/activity_plant.xml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,4 @@
136136
android:layout_marginLeft="29dp"
137137
android:layout_marginBottom="24dp"
138138
android:text="DELETE Plant" />
139-
140-
<com.google.android.material.floatingactionbutton.FloatingActionButton
141-
android:id="@+id/takeImage"
142-
android:layout_width="wrap_content"
143-
android:layout_height="wrap_content"
144-
android:layout_alignParentEnd="true"
145-
android:layout_alignParentRight="true"
146-
android:layout_alignParentBottom="true"
147-
android:layout_marginEnd="265dp"
148-
android:layout_marginRight="265dp"
149-
android:layout_marginBottom="441dp"
150-
android:clickable="true"
151-
app:srcCompat="@android:drawable/ic_menu_camera" />
152-
153139
</RelativeLayout>

390application/app/src/main/res/layout/fragment_insert_plant.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5+
xmlns:app="http://schemas.android.com/apk/res-auto"
56
android:orientation="vertical">
67

78
<Button
@@ -62,4 +63,17 @@
6263
android:layout_marginEnd="26dp"
6364
android:layout_marginRight="26dp" />
6465

66+
<com.google.android.material.floatingactionbutton.FloatingActionButton
67+
android:id="@+id/takeImage"
68+
android:layout_width="wrap_content"
69+
android:layout_height="wrap_content"
70+
android:layout_alignParentEnd="true"
71+
android:layout_alignParentRight="true"
72+
android:layout_alignParentBottom="true"
73+
android:layout_marginEnd="265dp"
74+
android:layout_marginRight="265dp"
75+
android:layout_marginBottom="441dp"
76+
android:clickable="true"
77+
app:srcCompat="@android:drawable/ic_menu_camera" />
78+
6579
</RelativeLayout>

0 commit comments

Comments
 (0)