Skip to content

Commit cfe2ecb

Browse files
author
Ray
committed
add upload and download, commit for testing
1 parent 27b0a15 commit cfe2ecb

File tree

10 files changed

+495
-14
lines changed

10 files changed

+495
-14
lines changed

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'com.google.gms.google-services'
23

34
android {
45
compileSdkVersion 29
@@ -23,6 +24,8 @@ dependencies {
2324
implementation fileTree(dir: 'libs', include: ['*.jar'])
2425
implementation 'androidx.appcompat:appcompat:1.0.2'
2526
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
27+
implementation 'com.google.firebase:firebase-storage:16.0.4'
28+
implementation 'com.google.firebase:firebase-auth:16.0.5'
2629
testImplementation 'junit:junit:4.12'
2730
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
2831
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

app/google-services.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"project_info": {
3+
"project_number": "647410336329",
4+
"firebase_url": "https://fir-storageapi-40a48.firebaseio.com",
5+
"project_id": "fir-storageapi-40a48",
6+
"storage_bucket": "fir-storageapi-40a48.appspot.com"
7+
},
8+
"client": [
9+
{
10+
"client_info": {
11+
"mobilesdk_app_id": "1:647410336329:android:55120c64ce063dae047598",
12+
"android_client_info": {
13+
"package_name": "com.mshlab.FirebaseStorageAPI_sample"
14+
}
15+
},
16+
"oauth_client": [
17+
{
18+
"client_id": "647410336329-3oj7jdqreaig68sgnm2il9he9359us7r.apps.googleusercontent.com",
19+
"client_type": 1,
20+
"android_info": {
21+
"package_name": "com.mshlab.FirebaseStorageAPI_sample",
22+
"certificate_hash": "f9c28fd349f4fb995b3b114459664cebea06e1dc"
23+
}
24+
},
25+
{
26+
"client_id": "647410336329-g2v3c015o54ttjm4heq88sp3qdsri7ca.apps.googleusercontent.com",
27+
"client_type": 3
28+
}
29+
],
30+
"api_key": [
31+
{
32+
"current_key": "AIzaSyCsiILtWexjRz4xx7ET7HFQ5cJgjxXhZxk"
33+
}
34+
],
35+
"services": {
36+
"appinvite_service": {
37+
"other_platform_oauth_client": [
38+
{
39+
"client_id": "647410336329-g2v3c015o54ttjm4heq88sp3qdsri7ca.apps.googleusercontent.com",
40+
"client_type": 3
41+
}
42+
]
43+
}
44+
}
45+
},
46+
{
47+
"client_info": {
48+
"mobilesdk_app_id": "1:647410336329:android:640edfbb6fe39be9047598",
49+
"android_client_info": {
50+
"package_name": "com.mshlab.mainactivity"
51+
}
52+
},
53+
"oauth_client": [
54+
{
55+
"client_id": "647410336329-kn1pi9cuaal2otksvrelml2odm4ssu2g.apps.googleusercontent.com",
56+
"client_type": 1,
57+
"android_info": {
58+
"package_name": "com.mshlab.mainactivity",
59+
"certificate_hash": "f9c28fd349f4fb995b3b114459664cebea06e1dc"
60+
}
61+
},
62+
{
63+
"client_id": "647410336329-g2v3c015o54ttjm4heq88sp3qdsri7ca.apps.googleusercontent.com",
64+
"client_type": 3
65+
}
66+
],
67+
"api_key": [
68+
{
69+
"current_key": "AIzaSyCsiILtWexjRz4xx7ET7HFQ5cJgjxXhZxk"
70+
}
71+
],
72+
"services": {
73+
"appinvite_service": {
74+
"other_platform_oauth_client": [
75+
{
76+
"client_id": "647410336329-g2v3c015o54ttjm4heq88sp3qdsri7ca.apps.googleusercontent.com",
77+
"client_type": 3
78+
}
79+
]
80+
}
81+
}
82+
}
83+
],
84+
"configuration_version": "1"
85+
}

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
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.mshlab.FirebaseStorageAPI_sample">
44

5+
<uses-permission android:name="android.permission.INTERNET" />
6+
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"
Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,92 @@
11
package com.mshlab.FirebaseStorageAPI_sample;
22

3+
import androidx.annotation.NonNull;
34
import androidx.appcompat.app.AppCompatActivity;
45

6+
import android.net.Uri;
57
import android.os.Bundle;
8+
import android.util.Log;
9+
import android.view.View;
10+
import android.widget.TextView;
611

12+
import com.google.android.gms.tasks.OnCompleteListener;
13+
import com.google.android.gms.tasks.OnSuccessListener;
14+
import com.google.android.gms.tasks.Task;
15+
import com.google.firebase.storage.FileDownloadTask;
16+
import com.google.firebase.storage.FirebaseStorage;
17+
import com.google.firebase.storage.StorageReference;
718
import com.mshlab.firebasestorageapi.FirebaseStorageAPI;
819

20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
924
public class MainActivity extends AppCompatActivity {
1025

26+
private String TAG = "MainActivity";
27+
File localFile;
28+
private InputStream inputStream;
29+
FirebaseStorageAPI firebaseStorageAPI;
30+
TextView statusTextView;
31+
1132
@Override
1233
protected void onCreate(Bundle savedInstanceState) {
1334
super.onCreate(savedInstanceState);
1435
setContentView(R.layout.activity_main);
1536

16-
FirebaseStorageAPI.showToast(this,"Hello World");
37+
statusTextView = findViewById(R.id.statusTextView);
38+
firebaseStorageAPI = new FirebaseStorageAPI.Builder()
39+
.setVisibleAcitivty(this)
40+
.allowCancel(false).build();
41+
42+
try {
43+
inputStream = this.getAssets().open("pic.png");
44+
} catch (IOException e) {
45+
e.printStackTrace();
46+
}
47+
48+
49+
try {
50+
localFile = File.createTempFile("images", "jpg");
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
55+
56+
}
57+
58+
public void downloadButton(View view) {
59+
StorageReference mStorageRef = FirebaseStorage.getInstance().getReference().getRoot().child("sample.png");
60+
firebaseStorageAPI.downloadToLocalPath(mStorageRef, localFile, new OnCompleteListener<FileDownloadTask.TaskSnapshot>() {
61+
@Override
62+
public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> task) {
63+
if (task.isComplete()) {
64+
Log.e(TAG, "Stream Size: " + localFile.length());
65+
statusTextView.setText("file downloaded successfully\n Size: " + localFile.length() + " Byte");
66+
67+
}
68+
}
69+
});
70+
}
71+
72+
73+
public void uploadButton(View view) {
74+
final StorageReference mStorageRef = FirebaseStorage.getInstance().getReference().getRoot().child("sample.png");
75+
firebaseStorageAPI.upload(inputStream, mStorageRef, new OnCompleteListener() {
76+
@Override
77+
public void onComplete(@NonNull Task task) {
78+
if (task.isSuccessful()) {
79+
mStorageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
80+
@Override
81+
public void onSuccess(Uri uri) {
82+
statusTextView.setText("file uploaded successfully\n File URL: " + uri.toString());
83+
84+
85+
}
86+
});
87+
88+
}
89+
}
90+
});
1791
}
1892
}
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7+
android:gravity="center"
8+
android:orientation="vertical"
79
tools:context=".MainActivity">
810

9-
<TextView
11+
12+
<Button
1013
android:layout_width="wrap_content"
1114
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13-
app:layout_constraintBottom_toBottomOf="parent"
14-
app:layout_constraintLeft_toLeftOf="parent"
15-
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
15+
android:onClick="uploadButton"
16+
android:layout_marginBottom="16dp"
17+
android:text="download file"></Button>
1718

18-
</androidx.constraintlayout.widget.ConstraintLayout>
19+
<Button
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:onClick="downloadButton"
23+
android:text="upload file"></Button>
24+
25+
<TextView
26+
android:id="@+id/statusTextView"
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"
29+
android:layout_marginTop="32dp"
30+
android:gravity="center"
31+
android:text="Ready to upload or download"
32+
android:textAppearance="@style/TextAppearance.AppCompat.Body2"></TextView>
33+
</LinearLayout>

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ buildscript {
88
}
99
dependencies {
1010
classpath 'com.android.tools.build:gradle:3.5.3'
11-
11+
classpath 'com.google.gms:google-services:4.2.0'
12+
1213
// NOTE: Do not place your application dependencies here; they belong
1314
// in the individual module build.gradle files
1415
}

firebasestorageapi/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ dependencies {
3131
testImplementation 'junit:junit:4.12'
3232
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
3333
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
34+
implementation 'com.google.firebase:firebase-auth:19.3.1'
35+
implementation 'com.google.firebase:firebase-storage:19.1.1'
3436
}
769 KB
Loading

0 commit comments

Comments
 (0)