2017-06-17 91 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個任務隊列中每個項目的。而當你把文件異步上傳文件,所以你正在做很多的異步任務。