【发布时间】:2019-03-20 19:17:54
【问题描述】:
在我的应用程序中,我有一个 ImageView,我想将与该 imageView 关联的图像保存到 Firebase 存储。这就是我所做的:
imageview.setDrawingCacheEnabled(true);
imageview.buildDrawingCache();
Bitmap bitmap = ((BitmapDrawable) imageview.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
StorageReference mStorage;
//CURRENT_USER,CCC,CAPITAL,TYPE are strings.
mStorage = FirebaseStorage.getInstance().getReference().child(CCC).child(Case+"/"+CAPITAL+"_"+TYPE.replaceAll(" ","_")+"_front.jpg");
mStorage.putBytes(data).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
//return null;
if (!task.isSuccessful()) {
throw task.getException();
}
return mStorage.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Log.d("save_document",downloadUri.toString());
} else {
Log.d("save_document","failed to upload image");
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("save_document","Exception while uploading the image"+e.toString());
}
});
图片永远不会上传到 Firebase 存储中,日志语句也永远不会执行,我哪里出错了?
【问题讨论】:
-
添加
addOnFailureListener()并检查你得到了什么异常 -
@SaurabhThorat 我添加了 onfailure 监听器,它也没有加入
标签: android firebase android-studio firebase-storage