Search in sources :

Example 1 with StorageTask

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();
    }
}
Also used : Continuation(com.google.android.gms.tasks.Continuation) Task(com.google.android.gms.tasks.Task) StorageTask(com.google.firebase.storage.StorageTask) StorageReference(com.google.firebase.storage.StorageReference) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) Uri(android.net.Uri) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) StorageTask(com.google.firebase.storage.StorageTask) NonNull(androidx.annotation.NonNull) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Aggregations

ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 NonNull (androidx.annotation.NonNull)1 Continuation (com.google.android.gms.tasks.Continuation)1 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)1 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)1 Task (com.google.android.gms.tasks.Task)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 StorageReference (com.google.firebase.storage.StorageReference)1 StorageTask (com.google.firebase.storage.StorageTask)1 HashMap (java.util.HashMap)1