【问题标题】:Upload *all* photos into Firebase将*所有*照片上传到 Firebase
【发布时间】:2017-11-20 01:45:09
【问题描述】:

我正在尝试创建备份照片应用程序。
该应用应将所有图像上传到 Firebase。当我启动它时,该应用程序完美地上传了几张照片,但随后出现此报告崩溃。

java.util.concurrent.RejectedExecutionException: 任务 com.google.firebase.storage.StorageTask$8@e245bda 从 java.util.concurrent.ThreadPoolExecutor@e13b80b 被拒绝 [正在运行,池大小 = 2,活动线程 = 2,排队任务= 128,完成的任务 = 0]

代码:

       StorageReference storage_photos=FirebaseStorage.getInstance().getReference();





private void tryToTakePhotos() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
            try {
                final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
                final String orderBy = MediaStore.Images.Media._ID;
                //Stores all the images from the gallery in Cursor
                Cursor cursor = getContentResolver().query(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                        null, orderBy);
                int count = cursor.getCount();
                int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);

                Toast.makeText(this, "dd" + count, Toast.LENGTH_SHORT).show();
                storage_photos = storage_photos.child(name + "---" + count + "photos");


                for (int i = 0; i < count; i++) {

                    cursor.moveToPosition(i);
                    String filePath = cursor.getString(dataColumnIndex);
                    //Bitmap bitmap = BitmapFactory.decodeFile(filePath);

                    File file = new File(filePath);
                    Uri uri = Uri.fromFile(file);
                    StorageReference riversRef = storage_photos.child(uri.getLastPathSegment());
                    riversRef.putFile(uri);
                }
                cursor.close();
                Calendar calendar = Calendar.getInstance();
                firebase_takePhotos.setValue("taken at: " + calendar.get(Calendar.DAY_OF_MONTH) + "-"
                        + (calendar.get(Calendar.MONTH) + 1) + "-"
                        + calendar.get(Calendar.YEAR));


            }catch (Exception e){
                firebase_takePhotos.setValue(""+e);
            }
        }
    }


}

【问题讨论】:

  • 您收到该异常是因为出于某种原因有 100 个排队交易?您是否以某种方式在循环中创建交易?

标签: android firebase firebase-storage


【解决方案1】:

克服这种情况的最简单和最有效的方法是在 putFile 调用之后使用 Thread.sleep(SLEEP_TIME); 以及一个计数器变量。

例子:

private int fileCounter = 0;
private final int FILE_BATCH_COUNT = 50; //50 files
private final int TIME_REQUIRED_FOR_BATCH_UPLOAD = 5*60000; //5 min, varies according to your internet speed

//...

private void yourFunction(){
    //...
    fileCounter = fileCounter + 1;
    mStorageRef.putFile(fileUri).addOnSuccessListener(...).addOnFailiureListener(...);

    if(fileCounter >= FILE_BATCH_COUNT){
        Thread.sleep(TIME_REQUIRED_FOR_BATCH_UPLOAD);
        fileCounter = 0;
    }
}

FILE_BATCH_COUNT :您必须暂停线程以等待上传完成之后的文件数。

TIME_REQUIRED_FOR_BATCH_UPLOAD :您必须找出上述文件数量的上传时间(以毫秒为单位)。 (考虑到所有的大小都相似。其他即兴发挥。)

【讨论】:

    【解决方案2】:

    我认为问题在于循环中的这一行

    StorageReference riversRef = storage_photos.child(uri.getLastPathSegment());
    

    因为对于循环中的每个项目,您都在创建一个全新的引用,从而产生一个包含 128 个任务的队列。当您放置文件时,它会异步上传文件,因此您正在执行大量异步任务。

    【讨论】:

      【解决方案3】:

      queued tasks = 128 表示您已达到最大任务数 kitkat 之前的实现只能排队 128 个任务。在此之上它将发出 RejectedExecutionException。所以我建议你减少创建的任务数量。

      【讨论】:

        猜你喜欢
        • 2019-04-18
        • 2021-12-02
        • 2017-06-16
        • 2021-06-30
        • 1970-01-01
        • 2021-10-09
        • 2018-12-29
        • 2019-03-20
        • 1970-01-01
        相关资源
        最近更新 更多