D
D
DJ_bot2017-11-23 13:10:58
Android
DJ_bot, 2017-11-23 13:10:58

Does not load the post if you do not fill in all the fields?

The post is not loaded on Firebase until all fields are filled in (shows an endless ProgressBar, if you remove it, nothing happens).

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);

    mStorage = FirebaseStorage.getInstance().getReference();
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");


    mSelectImage = (ImageButton) findViewById(R.id.imageSelect);

    mPostTitle =(EditText)findViewById(R.id.titleField);
    mPostDesc = (EditText)findViewById(R.id.descField);
    mSubmitBtn = (Button)findViewById(R.id.submitBtn);
    mProgress = new ProgressDialog(this);

    mSelectImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent,GALLERY_REQUEST);
        }
    });

    mSubmitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startposting();
        }
    });
}

private void startposting() {

    mProgress.setMessage("Uploading Post..");
    mProgress.show();

    final String title_val = mPostTitle.getText().toString().trim();
    final String desc_val = mPostDesc.getText().toString().trim();

    if(!TextUtils.isEmpty(title_val)&& !TextUtils.isEmpty(desc_val)&& mImageUri!=null){

        StorageReference filepath = mStorage.child("Blog_images").child(mImageUri.getLastPathSegment());

        filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                @SuppressWarnings("VisibleForTests") Uri downloadUri = taskSnapshot.getDownloadUrl();

                DatabaseReference newPost = mDatabase.push();
                newPost.child("title").setValue(title_val);
                newPost.child("desc").setValue(desc_val);
                newPost.child("image").setValue(downloadUri.toString());


                mProgress.dismiss();

                startActivity(new Intent(PostActivity.this, MainActivity.class));
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                @SuppressWarnings("VisibleForTests") double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();

                mProgress.setMessage("Uploading " + ((int) progress) + "%...");
                mProgress.show();
            }
        });
    }


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if ( requestCode == GALLERY_REQUEST && resultCode == RESULT_OK ){
        mImageUri = data.getData();
        mSelectImage.setImageURI(mImageUri);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question