2017-06-17 92 views
0

我正在尝试创建备份照片应用。
该应用应该将所有图片上传到Firebase。当我开始它的应用程序上传完美的几张照片,但随后崩溃与此报告。将*全部*照片上传到Firebase

java.util.concurrent.RejectedExecutionException:任务[email protected][email protected] [拒绝运行,池大小= 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); 
      } 
     } 
    } 


} 
+1

你正在得到这个异常,因为某些原因有100个排队交易?你是否以某种方式在循环中创建交易? –

回答

0

排队任务= 128表明您已经达到奇巧之前 实现可以排队只有128任务的最大任务数。超过它将发出RejectedExecutionException。所以我建议你减少创建的任务数量。

1

我认为这个问题是这一行中循环

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

因为在循环您正在制作一个全新的基准,造成128个任务队列中每个项目的。而当你把文件异步上传文件,所以你正在做很多的异步任务。