Created
April 14, 2017 22:52
-
-
Save mitchtabian/5c5259e1bfdab12774c43c0c797b2d98 to your computer and use it in GitHub Desktop.
Firebase-Save-Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:gravity="center_horizontal" | |
android:orientation="vertical" | |
tools:context="com.tabian.firebaseuploadimage.MainActivity"> | |
<ScrollView | |
android:id="@+id/login_form" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<LinearLayout | |
android:id="@+id/email_login_form" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<EditText | |
android:id="@+id/email" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Email" | |
android:inputType="textEmailAddress" | |
android:maxLines="1"/> | |
<EditText | |
android:id="@+id/password" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Password" | |
android:imeActionId="@+id/login" | |
android:imeOptions="actionUnspecified" | |
android:inputType="textPassword" | |
android:maxLines="1" | |
/> | |
<Button | |
android:id="@+id/email_sign_in_button" | |
style="?android:textAppearanceSmall" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="16dp" | |
android:text="Sign In" | |
android:textStyle="bold"/> | |
<Button | |
android:id="@+id/email_sign_out_button" | |
style="?android:textAppearanceSmall" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="16dp" | |
android:text="Sign out" | |
android:textStyle="bold"/> | |
<Button | |
android:id="@+id/upload_screen" | |
style="?android:textAppearanceSmall" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="16dp" | |
android:text="Upload" | |
android:textStyle="bold"/> | |
</LinearLayout> | |
</ScrollView> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.tabian.firebaseuploadimage"> | |
<uses-permission android:name="android.permission.INTERNET"></uses-permission> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.STORAGE" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN"/> | |
<category android:name="android.intent.category.LAUNCHER"/> | |
</intent-filter> | |
</activity> | |
<activity android:name=".UploadActivity"></activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tabian.firebaseuploadimage; | |
import android.content.Intent; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.auth.FirebaseUser; | |
public class MainActivity extends AppCompatActivity { | |
private static final String TAG = "MainActivity"; | |
private FirebaseAuth mAuth; | |
private FirebaseAuth.AuthStateListener mAuthListener; | |
// UI references. | |
private EditText mEmail, mPassword; | |
private Button btnSignIn, btnSignOut, btnUploadImage; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//declare buttons and edit texts in oncreate | |
mEmail = (EditText) findViewById(R.id.email); | |
mPassword = (EditText) findViewById(R.id.password); | |
btnSignIn = (Button) findViewById(R.id.email_sign_in_button); | |
btnSignOut = (Button) findViewById(R.id.email_sign_out_button); | |
btnUploadImage = (Button) findViewById(R.id.upload_screen); | |
mAuth = FirebaseAuth.getInstance(); | |
mAuthListener = new FirebaseAuth.AuthStateListener() { | |
@Override | |
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | |
FirebaseUser user = firebaseAuth.getCurrentUser(); | |
if (user != null) { | |
// User is signed in | |
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); | |
toastMessage("Successfully signed in with: " + user.getEmail()); | |
} else { | |
// User is signed out | |
Log.d(TAG, "onAuthStateChanged:signed_out"); | |
toastMessage("Successfully signed out."); | |
} | |
// ... | |
} | |
}; | |
btnSignIn.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
String email = mEmail.getText().toString(); | |
String pass = mPassword.getText().toString(); | |
if (!email.equals("") && !pass.equals("")) { | |
mAuth.signInWithEmailAndPassword(email, pass); | |
} else { | |
toastMessage("You didn't fill in all the fields."); | |
} | |
} | |
}); | |
btnSignOut.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
mAuth.signOut(); | |
toastMessage("Signing Out..."); | |
} | |
}); | |
btnUploadImage.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "onClick: Switching Activities."); | |
Intent intent = new Intent(MainActivity.this, UploadActivity.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
mAuth.addAuthStateListener(mAuthListener); | |
} | |
@Override | |
public void onStop() { | |
super.onStop(); | |
if (mAuthListener != null) { | |
mAuth.removeAuthStateListener(mAuthListener); | |
} | |
} | |
//add a toast to show when successfully signed in | |
/** | |
* customizable toast | |
* | |
* @param message | |
*/ | |
private void toastMessage(String message) { | |
Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<Button | |
android:text="Upload" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentTop="true" | |
android:layout_centerHorizontal="true" | |
android:layout_marginTop="13dp" | |
android:id="@+id/btnUploadImage"/> | |
<RelativeLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/btnUploadImage" | |
android:layout_centerHorizontal="true"> | |
<Button | |
android:text="Back" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="13dp" | |
android:layout_marginLeft="5dp" | |
android:id="@+id/btnBackImage" | |
/> | |
<Button | |
android:text="Next" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="13dp" | |
android:layout_marginLeft="5dp" | |
android:id="@+id/btnNextImage" | |
android:layout_toRightOf="@+id/btnBackImage"/> | |
</RelativeLayout> | |
<ImageView | |
android:layout_width="250dp" | |
android:layout_height="250dp" | |
android:layout_centerVertical="true" | |
android:layout_centerHorizontal="true" | |
android:id="@+id/uploadImage" | |
/> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="50dp" | |
android:layout_below="@+id/uploadImage" | |
android:id="@+id/imageName" | |
android:hint="Enter image name"/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tabian.firebaseuploadimage; | |
import android.app.ProgressDialog; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ImageView; | |
import android.widget.Toast; | |
import com.google.android.gms.tasks.OnFailureListener; | |
import com.google.android.gms.tasks.OnSuccessListener; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.auth.FirebaseUser; | |
import com.google.firebase.storage.FirebaseStorage; | |
import com.google.firebase.storage.StorageReference; | |
import com.google.firebase.storage.UploadTask; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
/** | |
* Created by User on 2/13/2017. | |
*/ | |
public class UploadActivity extends AppCompatActivity { | |
private static final String TAG = "UploadActivity"; | |
//declare variables | |
private ImageView image; | |
private EditText imageName; | |
private Button btnUpload,btnNext,btnBack; | |
private ProgressDialog mProgressDialog; | |
private final static int mWidth = 512; | |
private final static int mLength = 512; | |
private ArrayList<String> pathArray; | |
private int array_position; | |
private StorageReference mStorageRef; | |
private FirebaseAuth auth; | |
public UploadActivity() { | |
} | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.upload_layout); | |
image = (ImageView) findViewById(R.id.uploadImage); | |
btnBack = (Button) findViewById(R.id.btnBackImage); | |
btnNext = (Button) findViewById(R.id.btnNextImage); | |
btnUpload = (Button) findViewById(R.id.btnUploadImage); | |
imageName = (EditText) findViewById(R.id.imageName); | |
pathArray = new ArrayList<>(); | |
mProgressDialog = new ProgressDialog(UploadActivity.this); | |
auth = FirebaseAuth.getInstance(); | |
mStorageRef = FirebaseStorage.getInstance().getReference(); | |
checkFilePermissions(); | |
addFilePaths(); | |
btnBack.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(array_position > 0){ | |
Log.d(TAG, "onClick: Back an Image."); | |
array_position = array_position - 1; | |
loadImageFromStorage(); | |
} | |
} | |
}); | |
btnNext.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if(array_position < pathArray.size() - 1){ | |
Log.d(TAG, "onClick: Next Image."); | |
array_position = array_position + 1; | |
loadImageFromStorage(); | |
} | |
} | |
}); | |
btnUpload.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Log.d(TAG, "onClick: Uploading Image."); | |
mProgressDialog.setMessage("Uploading Image..."); | |
mProgressDialog.show(); | |
//get the signed in user | |
FirebaseUser user = auth.getCurrentUser(); | |
String userID = user.getUid(); | |
String name = imageName.getText().toString(); | |
if(!name.equals("")){ | |
Uri uri = Uri.fromFile(new File(pathArray.get(array_position))); | |
StorageReference storageReference = mStorageRef.child("images/users/" + userID + "/" + name + ".jpg"); | |
storageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { | |
@Override | |
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { | |
// Get a URL to the uploaded content | |
Uri downloadUrl = taskSnapshot.getDownloadUrl(); | |
toastMessage("Upload Success"); | |
mProgressDialog.dismiss(); | |
} | |
}).addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception e) { | |
toastMessage("Upload Failed"); | |
mProgressDialog.dismiss(); | |
} | |
}) | |
; | |
} | |
} | |
}); | |
} | |
/** | |
* Add the file paths you want to use into the array | |
*/ | |
private void addFilePaths(){ | |
Log.d(TAG, "addFilePaths: Adding file paths."); | |
String path = System.getenv("EXTERNAL_STORAGE"); | |
pathArray.add(path+"/Pictures/Portal/image1.jpg"); | |
pathArray.add(path+"/Pictures/Portal/image2.jpg"); | |
pathArray.add(path+"/Pictures/Portal/image3.jpg"); | |
loadImageFromStorage(); | |
} | |
private void loadImageFromStorage() | |
{ | |
try{ | |
String path = pathArray.get(array_position); | |
File f=new File(path, ""); | |
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f)); | |
image.setImageBitmap(b); | |
}catch (FileNotFoundException e){ | |
Log.e(TAG, "loadImageFromStorage: FileNotFoundException: " + e.getMessage() ); | |
} | |
} | |
private void checkFilePermissions() { | |
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){ | |
int permissionCheck = UploadActivity.this.checkSelfPermission("Manifest.permission.READ_EXTERNAL_STORAGE"); | |
permissionCheck += UploadActivity.this.checkSelfPermission("Manifest.permission.WRITE_EXTERNAL_STORAGE"); | |
if (permissionCheck != 0) { | |
this.requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1001); //Any number | |
} | |
}else{ | |
Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP."); | |
} | |
} | |
/** | |
* customizable toast | |
* @param message | |
*/ | |
private void toastMessage(String message){ | |
Toast.makeText(this,message,Toast.LENGTH_SHORT).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment