Skip to content

Commit

Permalink
add upload and download, commit for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray committed May 17, 2020
1 parent 27b0a15 commit cfe2ecb
Show file tree
Hide file tree
Showing 10 changed files with 495 additions and 14 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 29
Expand All @@ -23,6 +24,8 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-storage:16.0.4'
implementation 'com.google.firebase:firebase-auth:16.0.5'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
Expand Down
85 changes: 85 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"project_info": {
"project_number": "647410336329",
"firebase_url": "https://fir-storageapi-40a48.firebaseio.com",
"project_id": "fir-storageapi-40a48",
"storage_bucket": "fir-storageapi-40a48.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:647410336329:android:55120c64ce063dae047598",
"android_client_info": {
"package_name": "com.mshlab.FirebaseStorageAPI_sample"
}
},
"oauth_client": [
{
"client_id": "647410336329-3oj7jdqreaig68sgnm2il9he9359us7r.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "com.mshlab.FirebaseStorageAPI_sample",
"certificate_hash": "f9c28fd349f4fb995b3b114459664cebea06e1dc"
}
},
{
"client_id": "647410336329-g2v3c015o54ttjm4heq88sp3qdsri7ca.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCsiILtWexjRz4xx7ET7HFQ5cJgjxXhZxk"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "647410336329-g2v3c015o54ttjm4heq88sp3qdsri7ca.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
},
{
"client_info": {
"mobilesdk_app_id": "1:647410336329:android:640edfbb6fe39be9047598",
"android_client_info": {
"package_name": "com.mshlab.mainactivity"
}
},
"oauth_client": [
{
"client_id": "647410336329-kn1pi9cuaal2otksvrelml2odm4ssu2g.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "com.mshlab.mainactivity",
"certificate_hash": "f9c28fd349f4fb995b3b114459664cebea06e1dc"
}
},
{
"client_id": "647410336329-g2v3c015o54ttjm4heq88sp3qdsri7ca.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyCsiILtWexjRz4xx7ET7HFQ5cJgjxXhZxk"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "647410336329-g2v3c015o54ttjm4heq88sp3qdsri7ca.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mshlab.FirebaseStorageAPI_sample">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,92 @@
package com.mshlab.FirebaseStorageAPI_sample;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.mshlab.firebasestorageapi.FirebaseStorageAPI;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

private String TAG = "MainActivity";
File localFile;
private InputStream inputStream;
FirebaseStorageAPI firebaseStorageAPI;
TextView statusTextView;

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

FirebaseStorageAPI.showToast(this,"Hello World");
statusTextView = findViewById(R.id.statusTextView);
firebaseStorageAPI = new FirebaseStorageAPI.Builder()
.setVisibleAcitivty(this)
.allowCancel(false).build();

try {
inputStream = this.getAssets().open("pic.png");
} catch (IOException e) {
e.printStackTrace();
}


try {
localFile = File.createTempFile("images", "jpg");
} catch (IOException e) {
e.printStackTrace();
}


}

public void downloadButton(View view) {
StorageReference mStorageRef = FirebaseStorage.getInstance().getReference().getRoot().child("sample.png");
firebaseStorageAPI.downloadToLocalPath(mStorageRef, localFile, new OnCompleteListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> task) {
if (task.isComplete()) {
Log.e(TAG, "Stream Size: " + localFile.length());
statusTextView.setText("file downloaded successfully\n Size: " + localFile.length() + " Byte");

}
}
});
}


public void uploadButton(View view) {
final StorageReference mStorageRef = FirebaseStorage.getInstance().getReference().getRoot().child("sample.png");
firebaseStorageAPI.upload(inputStream, mStorageRef, new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
mStorageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
statusTextView.setText("file uploaded successfully\n File URL: " + uri.toString());


}
});

}
}
});
}
}
31 changes: 23 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:onClick="uploadButton"
android:layout_marginBottom="16dp"
android:text="download file"></Button>

</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="downloadButton"
android:text="upload file"></Button>

<TextView
android:id="@+id/statusTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:gravity="center"
android:text="Ready to upload or download"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"></TextView>
</LinearLayout>
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'

classpath 'com.google.gms:google-services:4.2.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
2 changes: 2 additions & 0 deletions firebasestorageapi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.google.firebase:firebase-auth:19.3.1'
implementation 'com.google.firebase:firebase-storage:19.1.1'
}
Binary file added firebasestorageapi/src/main/assets/pic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cfe2ecb

Please sign in to comment.