Skip to content

Instantly share code, notes, and snippets.

@varun7952
Created November 23, 2021 20:03
Show Gist options
  • Save varun7952/217cdcffc14e7784bbb59066471b180a to your computer and use it in GitHub Desktop.
Save varun7952/217cdcffc14e7784bbb59066471b180a to your computer and use it in GitHub Desktop.
Image uploading function which send images to the firebase storage in a loop (Multiple Image firebasae image upload )
// GETTING IMAGE PATHS FROM PHONE GALLERY IN ARRAYLIST(STRING TYPE)
private synchronized void UploadToFireBaseStorage(ArrayList<String> filePath,int type) {
String uploadFolder = null;
String attachmentType = null;
final ArrayList<String> multipleImages = new ArrayList<>();
Log.d(TAG,"Size of File at Upload Method "+filePath.size());
switch (type){
case 0:
uploadFolder = "Photos";
attachmentType = "Image";
break;
}
Uri fileUri = null;
for (String s : filePath) {
if (type == 0) {
if (s.contains(".jpg")) {
fileUri = Uri.fromFile(new File(s));
} else {
fileUri = Uri.parse(s);
}
}
StorageReference mStorageReference = FirebaseStorage.getInstance().getReference();
final StorageReference storageReference;
Log.d(TAG, "UploadToFireBaseStorage: StorageRef "+mStorageReference);
storageReference = mStorageReference.child(uploadFolder).child(fileUri.getLastPathSegment());
Log.d(TAG, "UploadToFireBaseStorage: Storage Reference "+storageReference);
Log.d(TAG, "UploadToFireBaseStorage: File URI = "+fileUri);
String finalAttachmentType = attachmentType;
storageReference.putFile(fileUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Log.d(TAG, "Image Uploaded ");
Log.d(TAG, "onSuccess: File Array == "+filePath.size()+" "+multipleImages.size());
multipleImages.add(uri.toString());
Log.d(TAG, "onSuccess: Image Uploaded Success ");
}
});
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = 100.0 * taskSnapshot.getBytesTransferred() / (double) taskSnapshot.getTotalByteCount();
Log.d(TAG, "Upload is " + progress + "% done");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: "+s);
Toast.makeText(Navigation_Drawer.this, "File Sending Failed", Toast.LENGTH_SHORT).show();
return;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment