我试图将对象上传到firebase。该对象包括照片,标题,详细信息和时间戳。我收到了错误:
java.lang.StackOverflowError: stack size 8MB
和
android.os.TransactionTooLargeException: data parcel size 1071868 bytes
我尝试上传照片时遇到错误,尽管它很小,只有108KB。
以下是我将照片和相关信息上传到firebase的方法:
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 ");
}
});
}
我在选择照片后获取图像的代码:
@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 :(得分: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);