将多个图像上载到firebase存储

时间:2017-06-11 03:48:30

标签: android firebase firebase-storage

我正在尝试将多个图片上传到firebase存储。 我为此写了以下代码: -

    private static void uploadImage(String imageUrl)
    {
        FirebaseStorage storage = FirebaseStorage.getInstance();
        final StorageReference storageRef = storage.getReferenceFromUrl("gs://image-store.appspot.com/");
        Uri file = Uri.fromFile(new File(imageUrl));
        String imageName = System.currentTimeMillis()+".jpg"; 
        StorageReference imageRef = storageRef.child("images").child(imageName);
        UploadTask uploadTask = imageRef.putFile(file);
    }

    private static uploadImages(String[] imageUrls)
    {
        for(imageUrl : imageUrls)
        {
            uploadImage(imageUrl)
        }
    }

当要上传的图片较少时,上述代码可以正常工作。但是当有大约100个图像时,它会创建几个异步线程,并出现以下错误: -

Exception java.util.concurrent.RejectedExecutionException: Task com.google.firebase.storage.StorageTask$5@ef10d00 rejected from java.util.concurrent.ThreadPoolExecutor@1c98b939[Running, pool size = 2, active threads = 2, queued tasks = 128, completed tasks = 59]
java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution (ThreadPoolExecutor.java:2042)
java.util.concurrent.ThreadPoolExecutor.reject (ThreadPoolExecutor.java:796)
java.util.concurrent.ThreadPoolExecutor.execute (ThreadPoolExecutor.java:1370)
com.google.firebase.storage.zzd.zzu ()
com.google.firebase.storage.UploadTask.schedule ()
com.google.firebase.storage.StorageTask.an ()
com.google.firebase.storage.StorageReference.putFile ()
com.indiacom.blanketfos.util.FirebaseUtil.uploadImage (FirebaseUtil.java:64)
com.indiacom.blanketfos.util.FirebaseUtil.uploadImages (FirebaseUtil.java:126)
com.indiacom.blanketfos.util.FirebaseUtil.syncRealmToFirebase (FirebaseUtil.java:33)
com.indiacom.blanketfos.ui.FormActivity.onOptionsItemSelected (FormActivity.java:406)
android.app.Activity.onMenuItemSelected (Activity.java:2983)
android.support.v4.app.FragmentActivity.onMenuItemSelected (FragmentActivity.java:408)
android.support.v7.app.AppCompatActivity.onMenuItemSelected (AppCompatActivity.java:195)
android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected (WindowCallbackWrapper.java:113)
android.support.v7.app.AppCompatDelegateImplV9.onMenuItemSelected (AppCompatDelegateImplV9.java:675)
android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected (MenuBuilder.java:822)
android.support.v7.view.menu.MenuItemImpl.invoke (MenuItemImpl.java:156)
android.support.v7.view.menu.MenuBuilder.performItemAction (MenuBuilder.java:969)
android.support.v7.view.menu.MenuBuilder.performItemAction (MenuBuilder.java:959)
android.support.v7.widget.ActionMenuView.invokeItem (ActionMenuView.java:623)
android.support.v7.view.menu.ActionMenuItemView.onClick (ActionMenuItemView.java:154)
android.view.View.performClick (View.java:4781)
android.view.View$PerformClick.run (View.java:19907)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:160)
android.app.ActivityThread.main (ActivityThread.java:5541)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:964)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:759)

所以我的问题是什么是将几个图像上传到firebase存储的最佳和最有效的方法,而不会遇到这样的问题。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,发现Firebase存储只接受免费帐户的最多100个并发上传。所以这就是我的所作所为并希望它有所帮助:

private static void uploadFS(final File[] files, final StorageReference certRef, int startIndex, int concurrent) {
    int cnt = 0;
    File file;
    Uri uriFile;
    StorageReference ref;
    while (cnt < concurrent) {
        file = files[startIndex];
        startIndex++;
        if (file.exists() && file.length() > 0) { // local cert file exits
            uriFile = Uri.fromFile(file);
            final int sIdx = startIndex;
            ref = certRef.child("cert/" + uriFile.getLastPathSegment());
            ref.putFile(uriFile).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    uploadFS(files, certRef, sIdx, 1);
                }
            });
        }
        cnt++;
    }
}
public static void uploadCert() {
    StorageReference rootStorageRef = getStorageRefRoot();
    if (rootStorageRef != null) { // loop through download folder
        File certFolder = new File(MainActivity.DOWNLOAD_FULL_PATH + "cert/");
        if (certFolder.isDirectory()) {
            File[] certFiles = certFolder.listFiles();
            uploadFS(certFiles, rootStorageRef, 0, 3);  // 3 is concurrent upload tasks
        }
    }
}