Firebase upload multiple images-------Note

AndriodStudio upload multiple file into Firebase(Storage)

一个简单的小功能,使用在现在着手准备的app的上传图片信息的部分,但是相对来说比较简陋哈哈。

#可以忽略滴胡扯
最近一直在用firebase,觉得贼好用,然后越用越觉得这有点是懒人开发平台一样,大部分的功能都给你写好了,只要按部就班地照着说明走就行,在马来西亚这边,大部分的freelance开发,甚至我们部分公司内部业务开发基本上都用firebase,用着用着问题就日渐突出。会发现,当firebase在处理相对大的数据的时候远远不及于SQL,所以同事开始提出,将业务逐渐迁移回SQL式存储,存在服务器里面,这意味着。。。。。。有人要开始从安装服务器开始进行一系列的探索和操作,虽然会有点莫名奇妙,但是他举了个例子,马来西亚有个连锁的小超市,名字忘了嘿嘿,就大概有1000家分店,计算每间分店每个月的数据是三百万,再乘个一千,这样的数据量firebase处理起来简直是巅峰状态(偶尔个延时5s,是不能容忍的)。。。国内就是从一开始就没有firebase这个平台,用啥还得问下在国内工作的师兄,目前我也不是很清楚

哦豁,废话太多,代码如下:
UploadFileActivity.java


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

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

public class UploadFileActivity extends AppCompatActivity {

    private  static  final int PICK_IMAGE = 1;
    Button upload,choose;
    TextView alert;

    ArrayList<Uri> ImageList = new ArrayList<Uri>();
    private Uri ImageUri;
    private ProgressDialog progressDialog;
    private int upload_count = 0;

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

        alert = findViewById(R.id.alert);
        upload = findViewById(R.id.upload_image);
        choose = findViewById(R.id.chooser);
        //show progressDialog messages
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Images Uploading Please wait.......");

        choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
                startActivityForResult(intent,PICK_IMAGE);

            }
        });

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                progressDialog.show();
                alert.setText("If Loading Takes too long please press the button again");

                StorageReference ImageFolder = FirebaseStorage.getInstance().getReference().child("ImageFolder");
                for(upload_count = 0; upload_count < ImageList.size(); upload_count++){
                    Uri IndividualImage = ImageList.get(upload_count);
                    final StorageReference ImageName = ImageFolder.child("Image" + IndividualImage.getLastPathSegment());

                    ImageName.putFile(IndividualImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                            ImageName.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                @Override
                                public void onSuccess(Uri uri) {
                                    String url = String.valueOf(uri);

                                    StoreLink(url);
                                }
                            });
                        }
                    });
                }

            }
        });
    }

    //Store the link of the images you upload
    private void StoreLink(String url){

        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("UserOne");
        HashMap<String,String> hashMap = new HashMap<>();

        hashMap.put("ImageLink",url);

        databaseReference.push().setValue(hashMap);

        progressDialog.dismiss();
        alert.setText("Image Upload successfully");
        upload.setVisibility(View.GONE);
        ImageList.clear();

    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Use requestCode to justify whether you finish the process "choose Images"
        if(requestCode == PICK_IMAGE){
            if(requestCode != RESULT_OK){
                if(data.getClipData() !=null){

                    int countClipData = data.getClipData().getItemCount();


                    int currentImageSelect  = 0;
                    while(currentImageSelect < countClipData){
                        ImageUri = data.getClipData().getItemAt(currentImageSelect).getUri();

                        ImageList.add(ImageUri);
                        currentImageSelect = currentImageSelect + 1;

                    }

                    alert.setVisibility(View.VISIBLE);
                    alert.setText("You have Select" + ImageList.size() + "Images");
                    choose.setVisibility(View.GONE);




                } else {
                    Toast.makeText(this,"Please Select Multiple Image",Toast.LENGTH_LONG).show();

                }
            }
        }






    }
}

activity_upload_file.xml

<?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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".UploadFileActivity">
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose Image"
        android:id="@+id/chooser"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Upload Image"
        android:id="@+id/upload_image"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="here will be alert"
        android:textSize="20sp"
        android:id="@+id/alert"
        android:textColor="@android:color/holo_red_light"
        android:textAlignment="center"
        android:layout_marginTop="50dp"
        android:visibility="gone"
        android:gravity="center_horizontal" />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值