在我的应用程序中,我有一个ImageView,我想将与该imageView相关的图像保存到Firebase Storage。这就是我所做的:
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存储中,也永远不会执行日志语句,我在哪里出错了?
答案 0 :(得分:0)
尝试一下
Print
答案 1 :(得分:0)
尝试一下。这是我的一个项目的一段代码。希望对您有所帮助。
private static final int Gallery_Pick_Fin = 1;
private Uri ImageUriFin;
ImageView addFinImg;
private String downloadUrlFin;
protected void onCreate(Bundle savedInstanceState) {
addFinImg = (ImageView)findViewById(R.id.add_cmnt_img);
addFinImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openGalleryFin();
}
});
postRecipe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressDialog.setMessage("Posting... \n Please Wait.. :)");
progressDialog.show();
final StorageReference filePath = postFinPicsRef.child("postFinPics").child(ImageUriFin.getLastPathSegment() + user.getUid());
filePath.putFile(ImageUriFin).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
downloadUrlFin = task.getResult().getDownloadUrl().toString();
dbRefUser = FirebaseDatabase.getInstance().getReference().child("users").child(user.getUid());
dbRefUser.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
HashMap<String, Object> userPosts = new HashMap<>();
userPosts.put("FinPic", downloadUrlFin);
dbRefPost.child(user.getUid()).child("" + aa).updateChildren(userPosts);
dbRefPostAll.child("Veg").child("" + aa).updateChildren(userPosts);
startActivity(new Intent(getApplicationContext(), PostTabActivity.class));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
});
}
});
}
private void openGalleryFin() {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,Gallery_Pick_Fin);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == Gallery_Pick_Fin && resultCode == RESULT_OK && data!=null){
ImageUriFin = data.getData();
addFinImg.setImageURI(ImageUriFin);
}
}