한윤범

add permission to access firebase storage

......@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.1"
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "com.example.user.firebaseauthdemo"
minSdkVersion 15
......@@ -28,10 +28,12 @@ dependencies {
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-auth:10.2.6'
compile 'com.google.android.gms:play-services-auth:10.2.6'
compile 'com.google.firebase:firebase-database:10.2.6'
compile 'com.google.firebase:firebase-storage:10.2.6'
compile 'com.google.firebase:firebase-auth:11.0.2'
compile 'com.google.firebase:firebase-core:11.0.2'
compile 'com.google.android.gms:play-services-auth:11.0.2'
compile 'com.google.firebase:firebase-database:11.0.2'
compile 'com.google.firebase:firebase-storage:11.0.2'
compile 'com.google.firebase:firebase-crash:11.0.2'
compile 'com.firebaseui:firebase-ui-database:0.4.0'
compile 'com.android.support:design:25.3.1'
testCompile 'junit:junit:4.12'
......
......@@ -3,6 +3,8 @@
package="com.example.user.firebaseauthdemo">
<user-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
......
......@@ -18,6 +18,7 @@ public class Main2Activity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
......
......@@ -31,6 +31,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
......
package com.example.user.firebaseauthdemo;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
public class PostActivity extends AppCompatActivity {
private ImageButton mSelectImage;
private EditText mPostTitle;
private EditText mPostDesc;
private Button mSubmitBtn;
private Uri mImageUri = null;
private static final int GALLARY_REQUEST = 1;
private static final int PERMISSIONS_REQUEST_READ_STORAGE = 100;
private StorageReference mStorage;
private ProgressDialog mProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSIONS_REQUEST_READ_STORAGE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
mSelectImage = (ImageButton)findViewById(R.id.imageSelect);
mPostTitle = (EditText) findViewById(R.id.titleField);
mPostDesc = (EditText) findViewById(R.id.descField);
mSubmitBtn = (Button) findViewById(R.id.submitBtn);
mStorage = FirebaseStorage.getInstance().getReference();
mProgress = new ProgressDialog(this);
mSelectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
......@@ -28,6 +62,34 @@ public class PostActivity extends AppCompatActivity {
startActivityForResult(gallaryIntent, GALLARY_REQUEST);
}
});
mSubmitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startPosting();
}
});
}
private void startPosting() {
mProgress.setMessage("Posting to Blog ... ");
String title_val = mPostTitle.getText().toString().trim();
String desc_val = mPostDesc.getText().toString().trim();
if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && mImageUri != null) {
mProgress.show();
StorageReference filepath = mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
mProgress.dismiss();
}
});
}
}
@Override
......@@ -35,8 +97,8 @@ public class PostActivity extends AppCompatActivity {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLARY_REQUEST && resultCode == RESULT_OK){
Uri imageUri = data.getData();
mSelectImage.setImageURI(imageUri);
mImageUri = data.getData();
mSelectImage.setImageURI(mImageUri);
}
}
}
......