한윤범

add permission to access firebase storage

...@@ -2,7 +2,7 @@ apply plugin: 'com.android.application' ...@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
2 2
3 android { 3 android {
4 compileSdkVersion 25 4 compileSdkVersion 25
5 - buildToolsVersion "26.0.1" 5 + buildToolsVersion '25.0.0'
6 defaultConfig { 6 defaultConfig {
7 applicationId "com.example.user.firebaseauthdemo" 7 applicationId "com.example.user.firebaseauthdemo"
8 minSdkVersion 15 8 minSdkVersion 15
...@@ -28,10 +28,12 @@ dependencies { ...@@ -28,10 +28,12 @@ dependencies {
28 compile 'com.android.support:recyclerview-v7:25.3.1' 28 compile 'com.android.support:recyclerview-v7:25.3.1'
29 compile 'com.android.support:cardview-v7:25.3.1' 29 compile 'com.android.support:cardview-v7:25.3.1'
30 compile 'com.android.support.constraint:constraint-layout:1.0.2' 30 compile 'com.android.support.constraint:constraint-layout:1.0.2'
31 - compile 'com.google.firebase:firebase-auth:10.2.6' 31 + compile 'com.google.firebase:firebase-auth:11.0.2'
32 - compile 'com.google.android.gms:play-services-auth:10.2.6' 32 + compile 'com.google.firebase:firebase-core:11.0.2'
33 - compile 'com.google.firebase:firebase-database:10.2.6' 33 + compile 'com.google.android.gms:play-services-auth:11.0.2'
34 - compile 'com.google.firebase:firebase-storage:10.2.6' 34 + compile 'com.google.firebase:firebase-database:11.0.2'
35 + compile 'com.google.firebase:firebase-storage:11.0.2'
36 + compile 'com.google.firebase:firebase-crash:11.0.2'
35 compile 'com.firebaseui:firebase-ui-database:0.4.0' 37 compile 'com.firebaseui:firebase-ui-database:0.4.0'
36 compile 'com.android.support:design:25.3.1' 38 compile 'com.android.support:design:25.3.1'
37 testCompile 'junit:junit:4.12' 39 testCompile 'junit:junit:4.12'
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
3 package="com.example.user.firebaseauthdemo"> 3 package="com.example.user.firebaseauthdemo">
4 4
5 <user-permission android:name="android.permission.INTERNET" /> 5 <user-permission android:name="android.permission.INTERNET" />
6 + <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
7 + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6 8
7 <application 9 <application
8 android:allowBackup="true" 10 android:allowBackup="true"
......
...@@ -18,6 +18,7 @@ public class Main2Activity extends AppCompatActivity ...@@ -18,6 +18,7 @@ public class Main2Activity extends AppCompatActivity
18 18
19 @Override 19 @Override
20 protected void onCreate(Bundle savedInstanceState) { 20 protected void onCreate(Bundle savedInstanceState) {
21 +
21 super.onCreate(savedInstanceState); 22 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main2); 23 setContentView(R.layout.activity_main2);
23 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 24 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
......
...@@ -31,6 +31,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -31,6 +31,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
31 31
32 @Override 32 @Override
33 protected void onCreate(Bundle savedInstanceState) { 33 protected void onCreate(Bundle savedInstanceState) {
34 +
34 super.onCreate(savedInstanceState); 35 super.onCreate(savedInstanceState);
35 setContentView(R.layout.activity_main); 36 setContentView(R.layout.activity_main);
36 37
......
1 package com.example.user.firebaseauthdemo; 1 package com.example.user.firebaseauthdemo;
2 2
3 +import android.Manifest;
4 +import android.app.ProgressDialog;
3 import android.content.Intent; 5 import android.content.Intent;
4 import android.net.Uri; 6 import android.net.Uri;
5 import android.os.Bundle; 7 import android.os.Bundle;
8 +import android.support.v4.app.ActivityCompat;
6 import android.support.v7.app.AppCompatActivity; 9 import android.support.v7.app.AppCompatActivity;
10 +import android.text.TextUtils;
7 import android.view.View; 11 import android.view.View;
12 +import android.widget.Button;
13 +import android.widget.EditText;
8 import android.widget.ImageButton; 14 import android.widget.ImageButton;
9 15
16 +import com.google.android.gms.tasks.OnSuccessListener;
17 +import com.google.firebase.storage.FirebaseStorage;
18 +import com.google.firebase.storage.StorageReference;
19 +import com.google.firebase.storage.UploadTask;
20 +
10 public class PostActivity extends AppCompatActivity { 21 public class PostActivity extends AppCompatActivity {
11 22
12 private ImageButton mSelectImage; 23 private ImageButton mSelectImage;
24 + private EditText mPostTitle;
25 + private EditText mPostDesc;
26 +
27 + private Button mSubmitBtn;
28 +
29 + private Uri mImageUri = null;
13 30
14 private static final int GALLARY_REQUEST = 1; 31 private static final int GALLARY_REQUEST = 1;
32 + private static final int PERMISSIONS_REQUEST_READ_STORAGE = 100;
33 +
34 + private StorageReference mStorage;
35 +
36 + private ProgressDialog mProgress;
15 37
16 @Override 38 @Override
17 protected void onCreate(Bundle savedInstanceState) { 39 protected void onCreate(Bundle savedInstanceState) {
40 +
41 + ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSIONS_REQUEST_READ_STORAGE);
42 +
18 super.onCreate(savedInstanceState); 43 super.onCreate(savedInstanceState);
19 setContentView(R.layout.activity_post); 44 setContentView(R.layout.activity_post);
20 45
21 mSelectImage = (ImageButton)findViewById(R.id.imageSelect); 46 mSelectImage = (ImageButton)findViewById(R.id.imageSelect);
22 47
48 + mPostTitle = (EditText) findViewById(R.id.titleField);
49 + mPostDesc = (EditText) findViewById(R.id.descField);
50 +
51 + mSubmitBtn = (Button) findViewById(R.id.submitBtn);
52 +
53 + mStorage = FirebaseStorage.getInstance().getReference();
54 +
55 + mProgress = new ProgressDialog(this);
56 +
23 mSelectImage.setOnClickListener(new View.OnClickListener() { 57 mSelectImage.setOnClickListener(new View.OnClickListener() {
24 @Override 58 @Override
25 public void onClick(View view) { 59 public void onClick(View view) {
...@@ -28,6 +62,34 @@ public class PostActivity extends AppCompatActivity { ...@@ -28,6 +62,34 @@ public class PostActivity extends AppCompatActivity {
28 startActivityForResult(gallaryIntent, GALLARY_REQUEST); 62 startActivityForResult(gallaryIntent, GALLARY_REQUEST);
29 } 63 }
30 }); 64 });
65 +
66 + mSubmitBtn.setOnClickListener(new View.OnClickListener() {
67 + @Override
68 + public void onClick(View view) {
69 + startPosting();
70 + }
71 + });
72 + }
73 +
74 + private void startPosting() {
75 +
76 + mProgress.setMessage("Posting to Blog ... ");
77 +
78 + String title_val = mPostTitle.getText().toString().trim();
79 + String desc_val = mPostDesc.getText().toString().trim();
80 +
81 + if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri != null) {
82 + mProgress.show();
83 +
84 + StorageReference filepath = mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());
85 + filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
86 + @Override
87 + public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
88 + Uri downloadUrl = taskSnapshot.getDownloadUrl();
89 + mProgress.dismiss();
90 + }
91 + });
92 + }
31 } 93 }
32 94
33 @Override 95 @Override
...@@ -35,8 +97,8 @@ public class PostActivity extends AppCompatActivity { ...@@ -35,8 +97,8 @@ public class PostActivity extends AppCompatActivity {
35 super.onActivityResult(requestCode, resultCode, data); 97 super.onActivityResult(requestCode, resultCode, data);
36 98
37 if(requestCode == GALLARY_REQUEST && resultCode == RESULT_OK){ 99 if(requestCode == GALLARY_REQUEST && resultCode == RESULT_OK){
38 - Uri imageUri = data.getData(); 100 + mImageUri = data.getData();
39 - mSelectImage.setImageURI(imageUri); 101 + mSelectImage.setImageURI(mImageUri);
40 } 102 }
41 } 103 }
42 } 104 }
......