将多个图像上传到Firebase存储



我使用firebase构建聊天应用程序,我想将多个图像发送到firebase存储。

使用此库

compile'com.github.darsh2:torusemagesElect:3474549'

在顶部

private StorageReference storageRef;
private FirebaseApp app;
private FirebaseStorage storage;

oncreate()方法

app = FirebaseApp.getInstance();
storage =FirebaseStorage.getInstance(app);

按钮单击操作

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

                Intent intent = new Intent(ChatActivity.this, AlbumSelectActivity.class);
                intent.putExtra(Constants.INTENT_EXTRA_LIMIT, 10);
                startActivityForResult(intent, Constants.REQUEST_CODE);
                pwindo1.dismiss();
            }
        });

活动结果

    if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK) {
        ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
        Uri uri = Uri.parse(String.valueOf(images));
        storageRef = storage.getReference("photos");
        final StorageReference photoRef = storageRef.child(uri.getLastPathSegment());
        photoRef.putFile(uri)
                .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Uri downloadUrl = taskSnapshot.getDownloadUrl();
                        String content = downloadUrl.toString();
                        if (content.length() > 0) {
                            editWriteMessage.setText("");
                            Message newMessage = new Message();
                            newMessage.text = content;
                            newMessage.idSender = StaticConfig.UID;
                            newMessage.idReceiver = roomId;
                            newMessage.timestamp = System.currentTimeMillis();
                            FirebaseDatabase.getInstance().getReference().child("message/" + roomId).push().setValue(newMessage);
                        }
                    }
                });
    }

变量

  • 私有静态final int pick_image = 1;
  • 按钮chooserbtn,uploaderBtn;
  • TextView Alert;
  • 私人Uri Imageuri;
  • arrayList Imagelist = new ArrayList();
  • private int upload_count = 0;
  • 私人progressDialog progressDialog;
  • arraylist urlstrings;

oncreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    uploaderBtn = findViewById(R.id.uploader);
    chooserBtn = findViewById(R.id.chooser);
    alert = findViewById(R.id.alert);
    progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setMessage("Uploading Images please Wait.........!!!!!!");
    chooserBtn.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);

        }
    });

上传器按钮

    uploaderBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            urlStrings = new ArrayList<>();
            progressDialog.show();
            alert.setText("If Loading Takes to long press 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("Images" + 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) {
                                                urlStrings.add(String.valueOf(uri));

                                                if (urlStrings.size() == ImageList.size()){
                                                    storeLink(urlStrings);
                                                }
                                            }
                                        }
                                );
                            }
                        }
                );

            }

        }
    });

}

这条线将帮助我们在一个节点下存储所有图像的链接

                                                if (urlStrings.size() == ImageList.size()){
                                                    storeLink(urlStrings);
                                                }

存储链接到firebase实时数据库

private void storeLink(ArrayList<String> urlStrings) {
    HashMap<String, String> hashMap = new HashMap<>();
    for (int i = 0; i <urlStrings.size() ; i++) {
        hashMap.put("ImgLink"+i, urlStrings.get(i));
    }
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("User");
    databaseReference.push().setValue(hashMap)
            .addOnCompleteListener(
                    new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                Toast.makeText(MainActivity.this, "Successfully Uplosded", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
            ).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(MainActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
    progressDialog.dismiss();
    alert.setText("Uploaded Successfully");
    uploaderBtn.setVisibility(View.GONE);
    ImageList.clear();
}

onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE) {
        if (resultCode == RESULT_OK) {

            if (data.getClipData() != null) {
                int countClipData = data.getClipData().getItemCount();
                int currentImageSlect = 0;
                while (currentImageSlect < countClipData) {
                    ImageUri = data.getClipData().getItemAt(currentImageSlect).getUri();
                    ImageList.add(ImageUri);
                    currentImageSlect = currentImageSlect + 1;
                }
                alert.setVisibility(View.VISIBLE);
                alert.setText("You have selected" + ImageList.size() + "Images");
                chooserBtn.setVisibility(View.GONE);

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

}

它工作正常。

 if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK) {
        ArrayList<Image> images = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
        Uri[] uri=new Uri[images.size()];
        for (int i =0 ; i < images.size(); i++) {
            uri[i] = Uri.parse("file://"+images.get(i).path);
            storageRef = storage.getReference("photos");
            final StorageReference ref = storageRef.child(uri[i].getLastPathSegment());
            ref.putFile(uri[i])
                    .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            Uri downloadUrl = taskSnapshot.getDownloadUrl();
                            String content = downloadUrl.toString();
                            if (content.length() > 0) {
                                editWriteMessage.setText("");
                                Message newMessage = new Message();
                                newMessage.text = content;
                                newMessage.idSender = StaticConfig.UID;
                                newMessage.idReceiver = roomId;
                                newMessage.timestamp = System.currentTimeMillis();
                                FirebaseDatabase.getInstance().getReference().child("message/" + roomId).push().setValue(newMessage);
                            }
                        }
                    });
        }
    }

此实用程序类i创建它,以在Coroutine的帮助下将多个图像上传到Firebase存储中。如果您有任何增强功能,请告诉我。

您需要首先添加这些依赖项。

实施'com.google.firebase:firebase-storage-ktx:19.1.1'

//firebase通过kotlinx-coroutines-play-servicesLibrary

为Coroutines增加了支持

实施" org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.1"

有关更多信息检查链接,github链接

object FirebaseUtils {
    suspend fun uploadPhotos(photosUri: ArrayList<File>): List<PhotoUrl> {
        val storageRef = Firebase.storage.reference
        val photosUrls = ArrayList<PhotoUrl>()
        val uploadedPhotosUriLink = withContext(CoroutineScope(Dispatchers.IO).coroutineContext) {
            (photosUri.indices).map { index ->
                async(Dispatchers.IO) {
                    uploadPhoto(storageRef, photosUri[index])
                }
            }
        }.awaitAll()
        uploadedPhotosUriLink.forEach { photoUriLink -> photosUrls.add(PhotoUrl(photoUriLink.toString())) }
        return photosUrls
    }

    private suspend fun uploadPhoto(storageRef: StorageReference, photoFile: File): Uri {
        val fileName = UUID.randomUUID().toString()
        val fileUri = Uri.fromFile(photoFile)
        return storageRef.child(fileName)
            .putFile(fileUri)
            .await()
            .storage
            .downloadUrl
            .await()
    }
}

返回列表imageUrls带有相同顺序的从画廊中选择的图像的不要用于每个URI

的循环

获得ArrayList<Uri> imageUriList并要现在将其上传到firebase存储,然后按照相同的顺序返回每个图像的URL列表,因此我在下面的方法上使用 recursion ,直到上传所有URI

 private void uploadImages(@NonNull ArrayList<String> imagesUrl,ArrayList<Uri> imageUriList) {
        StorageReference storageReference = FirebaseStorage.getInstance().getReference("Products").child(UUID.randomUUID().toString());
        Uri uri = imageUriList.get(imagesUrl.size());

        storageReference
                .putFile(uri).addOnSuccessListener(taskSnapshot ->
                storageReference.getDownloadUrl().addOnCompleteListener(task -> {
                    String url = Objects.requireNonNull(task.getResult()).toString();
                    imagesUrl.add(url);
                    //if same size so all image is uploaded, then sent list of url to to some method 
                    if(imagesUrl .size()  == imageUriList.size()){
                        allImageUploadedNow(imagesUrl);
                    }else {
                        uploadImages(imagesUrl);
                    }
                }))
                .addOnFailureListener(e -> {
                    Log.e("OnFailureImageListener", Objects.requireNonNull(e.getMessage()));
                   
                   //some image is fails to upload 
                
                });

    }

因此,现在从OnActivityResult获取imageUriList值后,我们可以通过此调用方法;

uploadImages(new ArrayList<>(),uriFilesList);

并且完成所有图像时,您可以在内部处理

 private void allImageUploadedNow(ArrayList<String> imagesUrl){
  //handle imagesUrl
}

在我将多个图像上传到firebase的情况下,我已使用foreach()。foreach()方法帮助我成功地上传了图像


    import { getStorage, ref, uploadBytesResumable, getDownloadURL } from "firebase/storage";
    const Product =()=>{
    const [file, setFile] =useState([])
    Object.values(file).forEach(val => {
    const storage = getStorage();
    const storageRef = ref(storage, 'images/val.name');
    const uploadTask = uploadBytesResumable(storageRef, val);
    uploadTask.on('state_changed', 
      (snapshot) => {
         number of bytes to be uploaded
        const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
        console.log('Upload is ' + progress + '% done');
        switch (snapshot.state) {
          case 'paused':
            console.log('Upload is paused');
            break;
          case 'running':
            console.log('Upload is running');
            break;
        }
      }, 
      (error) => {
      }, 
      () => {
        getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
          console.log('File available at', downloadURL);
        });
      }
    );
    })
    return (
    <button onClick={(e) => setFile(e.target.files)}/>
    )
    }

相关内容

  • 没有找到相关文章

最新更新