2017-05-11 99 views
0

我試圖上傳一個對象到firebase。該對象包含照片,標題,詳細信息和時間戳。我發現了錯誤:獲取android.os.TransactionTooLargeException當上傳到Firebase

java.lang.StackOverflowError: stack size 8MB 

android.os.TransactionTooLargeException: data parcel size 1071868 bytes 

試圖將照片上傳儘管它是很小,只有108KB,當我得到的錯誤。

這裏是我上傳的照片和相關信息,以火力的方法:

private void publishNews() { 

    final String title = etTitle.getText().toString(); 
    final String detail = etDetail.getText().toString(); 

    StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("news"); 
    StorageReference imgRef = storageReference.child(imgUri.getLastPathSegment()); 

    final UploadTask uploadTask = imgRef.putFile(imgUri); 

    // Show alert for loading 
    android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(getActivity(), R.style.CustomTheme_Dialog_Upload) 
      .setTitle("Uploading...") 
      .setMessage("1 %") 

      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
         /* Dismiss the dialog */ 
        uploadTask.cancel(); 
        dialog.dismiss(); 
       } 
      }) 
      .setIcon(R.drawable.ic_cloud_upload_black_24dp); 

    final android.app.AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 
    alertDialog.setCancelable(false); 

    // Upload file to firebase on storageRef location 
    uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { 
     @Override 
     public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { 
      @SuppressWarnings("VisibleForTests") 
      double progress = (100.0 * taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount(); 
      alertDialog.setMessage((int) progress + " %"); 
     } 
    }) 
      .addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() { 
       @Override 
       public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 

        if(taskSnapshot.getDownloadUrl() == null) { 
         alertDialog.setMessage("Sorry there was a problem"); 
         return; 
        } 
        alertDialog.setMessage("Saving..."); 

        Uri photoDownloadUrl = taskSnapshot.getDownloadUrl(); 

        final HashMap<String, Object> newsObj = new HashMap<>(); 
        newsObj.put("photoUrl", photoDownloadUrl); 
        newsObj.put("title", title); 
        newsObj.put("detail", detail); 
        newsObj.put("timestamp", ServerValue.TIMESTAMP); 

        FirebaseDatabase database = FirebaseDatabase.getInstance(); 
        database.getReference().child("news").push().setValue(newsObj).addOnCompleteListener(new OnCompleteListener<Void>() { 
         @Override 
         public void onComplete(@NonNull Task<Void> task) { 
          alertDialog.dismiss(); 
         } 
        }); 
       } 
      }) 

      .addOnFailureListener(new OnFailureListener() { 
       @Override 
       public void onFailure(@NonNull Exception e) { 
        alertDialog.setIcon(android.R.drawable.ic_dialog_alert); 
        alertDialog.setMessage("Failed to upload"); 
       } 
      }) 

      .addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() { 
       @Override 
       public void onPaused(UploadTask.TaskSnapshot taskSnapshot) { 
        alertDialog.setIcon(android.R.drawable.ic_dialog_alert); 
        alertDialog.setMessage("Upload paused "); 
       } 
      }); 

} 

從哪裏獲得的圖像URI挑選照片後的代碼:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    // After picking a photo 
    if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) { 

     imgUri = data.getData(); 

     Glide.with(getActivity()).load(imgUri).into(imbAddPhoto); 
    } 
} 

奇怪的是我曾經能夠成功地做到這一點,但後來我一直得到這個錯誤。感謝您的任何建議。

+0

「我試圖上傳照片時出現錯誤,儘管它很小,只有108KB」 - 這可能是* disk *上照片的大小。重要的是內存中的'Bitmap' *的大小。照片被壓縮在磁盤上。除此之外,請編輯您的問題併發布您的'OutOfMemoryError'的整個Java堆棧跟蹤。 – CommonsWare

+0

謝謝你的幫助,我想我找出了問題所在。這是photoDownloadUrl,它必須是一個字符串而不是Uri。 – fullMoon

回答

1

問題是photoDownloadUrl必須是字符串而不是Uri。這是正確的版本:

    String photoDownloadUrl = String.valueOf(taskSnapshot.getDownloadUrl()); 

        final HashMap<String, Object> newsObj = new HashMap<>(); 
        newsObj.put("photoUrl", photoDownloadUrl); 
        newsObj.put("title", title); 
        newsObj.put("detail", detail); 
        newsObj.put("timestamp", ServerValue.TIMESTAMP); 
+0

它的工作......謝謝 –

相關問題