Skip to content

Commit 599d4b7

Browse files
committed
Uploading Images to Firebase Storage
1 parent e5a1037 commit 599d4b7

4 files changed

Lines changed: 81 additions & 1 deletion

File tree

0 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
2828
implementation 'com.google.firebase:firebase-core:15.0.0'
2929
implementation 'com.firebaseui:firebase-ui-auth:3.3.1'
30+
implementation 'com.google.firebase:firebase-storage:15.0.0'
3031
}
3132

3233
apply plugin: 'com.google.gms.google-services'

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.dragosholban.myinstagramapp">
44

5+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
6+
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"
@@ -18,7 +20,9 @@
1820
<category android:name="android.intent.category.LAUNCHER" />
1921
</intent-filter>
2022
</activity>
21-
<activity android:name=".FeedActivity"></activity>
23+
<activity
24+
android:name=".FeedActivity"
25+
android:screenOrientation="portrait"></activity>
2226
</application>
2327

2428
</manifest>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,93 @@
11
package com.dragosholban.myinstagramapp;
22

3+
import android.Manifest;
4+
import android.content.Intent;
5+
import android.content.pm.PackageManager;
6+
import android.net.Uri;
7+
import android.support.annotation.NonNull;
8+
import android.support.v4.app.ActivityCompat;
9+
import android.support.v4.content.ContextCompat;
310
import android.support.v7.app.AppCompatActivity;
411
import android.os.Bundle;
512
import android.view.View;
13+
import android.widget.Toast;
14+
15+
import com.google.android.gms.tasks.OnFailureListener;
16+
import com.google.android.gms.tasks.OnSuccessListener;
17+
import com.google.firebase.auth.FirebaseAuth;
18+
import com.google.firebase.auth.FirebaseUser;
19+
import com.google.firebase.storage.FirebaseStorage;
20+
import com.google.firebase.storage.StorageReference;
21+
import com.google.firebase.storage.UploadTask;
22+
23+
import java.text.SimpleDateFormat;
24+
import java.util.Date;
625

726
public class FeedActivity extends AppCompatActivity {
827

28+
FirebaseUser fbUser;
29+
30+
static final int RC_PERMISSION_READ_EXTERNAL_STORAGE = 1;
31+
static final int RC_IMAGE_GALLERY = 2;
32+
933
@Override
1034
protected void onCreate(Bundle savedInstanceState) {
1135
super.onCreate(savedInstanceState);
1236
setContentView(R.layout.activity_feed);
37+
38+
fbUser = FirebaseAuth.getInstance().getCurrentUser();
39+
if (fbUser == null) {
40+
finish();
41+
}
1342
}
1443

1544
public void uploadImage(View view) {
45+
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
46+
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, RC_PERMISSION_READ_EXTERNAL_STORAGE);
47+
} else {
48+
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
49+
intent.setType("image/*");
50+
startActivityForResult(intent, RC_IMAGE_GALLERY);
51+
}
52+
}
53+
54+
@Override
55+
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
56+
if (requestCode == RC_PERMISSION_READ_EXTERNAL_STORAGE) {
57+
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
58+
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
59+
intent.setType("image/*");
60+
startActivityForResult(intent, RC_IMAGE_GALLERY);
61+
}
62+
}
63+
}
64+
65+
@Override
66+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
67+
if (requestCode == RC_IMAGE_GALLERY && resultCode == RESULT_OK) {
68+
Uri uri = data.getData();
69+
70+
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
71+
StorageReference imagesRef = storageRef.child("images");
72+
StorageReference userRef = imagesRef.child(fbUser.getUid());
73+
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
74+
String filename = fbUser.getUid() + "_" + timeStamp;
75+
StorageReference fileRef = userRef.child(filename);
1676

77+
UploadTask uploadTask = fileRef.putFile(uri);
78+
uploadTask.addOnFailureListener(new OnFailureListener() {
79+
@Override
80+
public void onFailure(@NonNull Exception exception) {
81+
// Handle unsuccessful uploads
82+
Toast.makeText(FeedActivity.this, "Upload failed!\n" + exception.getMessage(), Toast.LENGTH_LONG).show();
83+
}
84+
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
85+
@Override
86+
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
87+
Uri downloadUrl = taskSnapshot.getDownloadUrl();
88+
Toast.makeText(FeedActivity.this, "Upload finished!", Toast.LENGTH_SHORT).show();
89+
}
90+
});
91+
}
1792
}
1893
}

0 commit comments

Comments
 (0)