
在本文中,您将学习如何从 Android应用程序的Firebase存储中提取图像 。
火力基地
Firebase是Firebase在2011年开发并于2014年被Google收购的移动和Web应用程序开发平台。 截至2018年10月,Firebase平台拥有18种产品,可用于150万个应用程序中。 它有助于快速开发高质量的应用程序,扩大用户群并赚更多钱。
滑翔
Glide是一个库,用于在由Bump Tech开发并由Google推荐的Android应用程序中下载图像。 它被许多Google开源项目使用,包括官方的Google I / O 2014应用程序。Glide支持下载,解码和显示图像,视频以及GIF动画。
配置Firebase
让我们为我们的Android项目设置Firebase。


- 您将看到一个标题为“将Firebase添加到您的Android应用程序”的页面 。

例如→ com.example.retrieving_images_from_firebase
。

- 打开项目级别的gradle文件。 将此依赖项添加到依赖项块中:
classpath "com.google.gms:google-services:3.0.0"
所以应该看起来:

- 打开应用程序级别的gradle文件。 添加依赖项:
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.squareup.picasso:picasso:2.71828' testImplementation 'junit:junit:4.12' implementation 'com.github.bumptech.glide:glide:4.7.1' compile 'com.android.support.constraint:constraint-layout:1.1.3' compile 'com.google.firebase:firebase-database:11.0.2' compile 'com.google.firebase:firebase-storage:11.0.2' compile 'com.google.firebase:firebase-auth:11.0.2' compile 'com.firebaseui:firebase-ui-database:2.1.0'
所以应该看起来:

- 现在,将packagepingOptions添加到 buildTypes块的底部:
packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE-FIREBASE.txt' exclude 'META-INF/NOTICE' }
所以应该看起来:

现在,单击数据库→规则 。 添加以下行:
service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write; } } }
单击存储→规则 。 添加以下行:
service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } }
- 现在,单击存储→文件 。 使用“上传文件”按钮下载图像。

- 单击任何上传的图像。 然后在右下角,您将找到Download URL1 。 复制此。

Activity_main.xml
创建一个将在其中显示图像的ImageView。
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RETRIEVE FROM FIREBASE" android:gravity="center" android:textSize="30dp" android:textColor="#000000"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/image"/> </LinearLayout>
MainActivity.java
package com.example.retrieving_images_from_firebase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import com.bumptech.glide.Glide; public class MainActivity extends AppCompatActivity { ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView=findViewById(R.id.image);
恭喜你! 现在,您可以启动应用程序。
启动应用程序后,您将看到您的图像。

您可以从GitHub上的存储库下载所有代码。