我对Firebase存储有一个奇怪的问题。最近,我能够完美地上传文件。但现在,即使上传尚未完成,上传似乎也会触发。上传在控制台上显示0B,文件扩展名为application / octet-stream。我正在上传我之前上传的相同图片,但我没有对我的代码进行更改。
这是我的代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE) {
if (data == null) {
//Display an error
return;
}
try {
//UPLOAD IMAGE
InputStream inputStream = getActivity().getContentResolver().openInputStream(data.getData());
if(inputStream != null) {
img_upload_school_pic.setImageBitmap(BitmapFactory.decodeStream(new BufferedInputStream(inputStream)));
img_upload_school_pic.setVisibility(View.INVISIBLE);
//SHOW PROGRESSBAR
prg_upload.setVisibility(View.VISIBLE);
//UPLOAD IMAGE
ImageUploader uploader = new ImageUploader();
uploader.uploadSchoolDp(inputStream, "sample.jpg", img_upload_school_pic, prg_upload);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
上传者类的代码
public class ImageUploader {
private StorageReference rootRef = FirebaseStorage.getInstance().getReference().getRoot();
public void uploadSchoolDp(final InputStream inputStream, String key,
final ImageView img_upload_school_pic, final ProgressBar prg_upload) {
StorageReference schools_dp_ref = rootRef.child("samplefoler");
StorageReference img_df_ref = schools_dp_ref.child(key);
UploadTask uploadTask = img_df_ref.putStream(inputStream);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//HIDE PROGRESSBAR AND SHOW IMAGEVIEW
img_upload_school_pic.setVisibility(View.VISIBLE);
prg_upload.setVisibility(View.INVISIBLE);
}
});
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
//
// uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
// @Override
// public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
// txt_progress.setText(taskSnapshot.getBytesTransferred() + " of " +
// taskSnapshot.getTotalByteCount());
// Log.d(null, Double.toString(taskSnapshot.getTotalByteCount()));
// }
// });
}
}
还有一件事。 taskSnapshot.getTotalByteCount()返回-1。我无法使用它来显示上传的状态,所以我暂时将其评论出来。
答案 0 :(得分:0)
似乎文件在控制台上没有正确显示,但它们上传得很好,因为我可以使用生成的下载URL查看图像。目前,我会忽略这个问题。