use of com.google.firebase.storage.StorageTask in project Instagram-app by akshay8560.
the class PostActivity method upload.
private void upload() {
ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Uploading");
pd.show();
if (imageuri != null) {
StorageReference filepath = FirebaseStorage.getInstance().getReference("Posts").child(System.currentTimeMillis() + "." + getFileExtension(imageuri));
StorageTask uploadTask = filepath.putFile(imageuri);
uploadTask.continueWithTask(new Continuation() {
@Override
public Object then(@NonNull Task task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return filepath.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
Uri downloadUri = task.getResult();
imageurl = downloadUri.toString();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
String postId = reference.push().getKey();
HashMap<String, Object> map = new HashMap<>();
map.put("postid", postId);
map.put("postimageurl", imageurl);
map.put("description", description.getText().toString());
map.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.child(postId).setValue(map);
DatabaseReference mHashTagRef = FirebaseDatabase.getInstance().getReference().child("HashTags");
List<String> hashTags = description.getHashtags();
if (!hashTags.isEmpty()) {
for (String tag : hashTags) {
map.clear();
map.put("tag", tag.toLowerCase());
map.put("postid", postId);
mHashTagRef.child(tag.toLowerCase()).setValue(map);
}
}
pd.dismiss();
startActivity(new Intent(PostActivity.this, MainActivity.class));
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(PostActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(this, "No Image was selected", Toast.LENGTH_SHORT).show();
}
}
Aggregations