Permalink
Browse files

Uploading Images to Firebase Storage

  • Loading branch information...
dragosholban committed May 13, 2018
1 parent e5a1037 commit 599d4b770f51ff1aab42c51279fb5232016cfcb5
Binary file not shown.
@@ -27,6 +27,7 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:15.0.0'
implementation 'com.firebaseui:firebase-ui-auth:3.3.1'
implementation 'com.google.firebase:firebase-storage:15.0.0'
}

apply plugin: 'com.google.gms.google-services'
@@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dragosholban.myinstagramapp">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@@ -18,7 +20,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FeedActivity"></activity>
<activity
android:name=".FeedActivity"
android:screenOrientation="portrait"></activity>
</application>

</manifest>
@@ -1,18 +1,93 @@
package com.dragosholban.myinstagramapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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.text.SimpleDateFormat;
import java.util.Date;

public class FeedActivity extends AppCompatActivity {

FirebaseUser fbUser;

static final int RC_PERMISSION_READ_EXTERNAL_STORAGE = 1;
static final int RC_IMAGE_GALLERY = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);

fbUser = FirebaseAuth.getInstance().getCurrentUser();
if (fbUser == null) {
finish();
}
}

public void uploadImage(View view) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, RC_PERMISSION_READ_EXTERNAL_STORAGE);
} else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, RC_IMAGE_GALLERY);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == RC_PERMISSION_READ_EXTERNAL_STORAGE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, RC_IMAGE_GALLERY);
}
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_IMAGE_GALLERY && resultCode == RESULT_OK) {
Uri uri = data.getData();

StorageReference storageRef = FirebaseStorage.getInstance().getReference();
StorageReference imagesRef = storageRef.child("images");
StorageReference userRef = imagesRef.child(fbUser.getUid());
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String filename = fbUser.getUid() + "_" + timeStamp;
StorageReference fileRef = userRef.child(filename);

UploadTask uploadTask = fileRef.putFile(uri);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Toast.makeText(FeedActivity.this, "Upload failed!\n" + exception.getMessage(), Toast.LENGTH_LONG).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Toast.makeText(FeedActivity.this, "Upload finished!", Toast.LENGTH_SHORT).show();
}
});
}
}
}

0 comments on commit 599d4b7

Please sign in to comment.