Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sendDataFirebase.java
private void savePrediction(Prediction prediction) {
Log.d(TAG,"savePrediction");
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference databaseReference = firebaseDatabase.getReference();
databaseReference.child("prediction").child(prediction.getId()).setValue(prediction);
}
private void uploadImage(String absolutePath, final Prediction prediction) {
Uri file = Uri.fromFile(new File(absolutePath));
Long timestamp = System.currentTimeMillis() / 1000;
StorageReference imageRef = storageReference.child("images/"+timestamp.toString()+".jpg");
prediction.setTimestamp(timestamp);
imageRef.putFile(file)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Log.d(TAG,"image uploaded with success "+downloadUrl.toString());
prediction.setImageDownloadUrl(downloadUrl.toString());
savePrediction(prediction);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
exception.printStackTrace();
Log.d(TAG,"error cause "+exception.getCause().toString());
Log.d(TAG,"error on upload image "+exception.toString());
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment