Skip to content
Snippets Groups Projects
Commit ead41e5b authored by kishoreraju2's avatar kishoreraju2
Browse files

Upload Images to firebase

parent 49fbfc15
No related branches found
No related tags found
No related merge requests found
......@@ -32,9 +32,11 @@ dependencies {
compile 'com.roughike:bottom-bar:2.0.2'
compile 'com.android.volley:volley:1.0.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-storage:10.2.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
\ No newline at end of file
......@@ -2,11 +2,14 @@ package com.mapps.seproject;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
......@@ -18,6 +21,15 @@ import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.IOException;
import static android.app.Activity.RESULT_OK;
/**
......@@ -31,9 +43,13 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
Spinner dropdown;
TextView emailText;
private StorageReference mStorageRef;
Button bAddImage;
View view;
Uri imageUri;
private Uri imageUri;
Button upload;
private static int RESULT_LOAD_IMAGE = 1;
private static int RESULT_MAIL = 2;
......@@ -59,10 +75,14 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
emailText = (TextView) view.findViewById(R.id.tvEmailMessage);
bAddImage = (Button) view.findViewById(R.id.bAddImage);
b_get = (Button) view.findViewById(R.id.button2);
upload = (Button) view.findViewById(R.id.upload_image);
mStorageRef = FirebaseStorage.getInstance().getReference();
bAddImage.setOnClickListener(this);
bComposeMail.setOnClickListener(this);
b_get.setOnClickListener(this);
upload.setOnClickListener(this);
return view;
}
......@@ -148,6 +168,11 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
}
//Upload Image
if(v == upload){
uploadFile();
}
//Location
if(v == b_get){
gps = new TrackGPS(getActivity());
......@@ -174,6 +199,11 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
......@@ -190,6 +220,59 @@ public class ComposeFragment extends Fragment implements View.OnClickListener{
}
//Upload image
private void uploadFile() {
//if there is a file to upload
if (imageUri != null) {
//displaying a progress dialog while upload is going on
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle("Uploading");
progressDialog.show();
StorageReference riversRef = mStorageRef.child("images/pic.jpg");
riversRef.putFile(imageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//if the upload is successfull
//hiding the progress dialog
progressDialog.dismiss();
//and displaying a success toast
Toast.makeText(getActivity().getBaseContext(), "File Uploaded ", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
//if the upload is not successfull
//hiding the progress dialog
progressDialog.dismiss();
//and displaying error message
Toast.makeText(getActivity().getBaseContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@SuppressWarnings("VisibleForTests")
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
//calculating progress percentage
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//displaying percentage in progress dialog
progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
}
});
}
//if there is not any file
else {
//you can display an error toast
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
......
......@@ -28,10 +28,9 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bComposeMail"
android:layout_marginBottom="28dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="16dp"
android:layout_alignParentEnd="true" />
android:layout_alignParentStart="true" />
<Button
android:text="Add Image"
......@@ -39,7 +38,8 @@
android:layout_height="wrap_content"
android:id="@+id/bAddImage"
android:layout_above="@+id/bComposeMail"
android:layout_alignParentStart="true" />
android:layout_alignParentStart="true"
android:layout_marginBottom="12dp" />
<EditText
android:layout_width="match_parent"
......@@ -61,6 +61,15 @@
android:layout_centerHorizontal="true"
android:text="Get Location" />
<Button
android:id="@+id/upload_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Upload Image"
android:layout_alignBaseline="@+id/bComposeMail"
android:layout_alignBottom="@+id/bComposeMail"
android:layout_toEndOf="@+id/button2" />
</RelativeLayout>
</FrameLayout>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment