数据未添加到数据库

时间:2017-10-23 16:12:54

标签: android firebase firebase-realtime-database

此代码基本上有一个图像按钮和2个编辑文本框,我希望将其存储在数据库中。 但进度条一直在旋转,没有任何内容被上传到数据库。 但是在连接到Firebase时没有显示任何错误。在执行时,我面临这个问题。 并且图像按钮仅适用于驱动器上载,尽管我已在清单中声明了对外部存储的许可。

任何可能的错误?

评论是否需要任何等级。

 package com.example.acer.simpleblog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.io.IOException;

import static android.R.attr.data;
import static android.app.PendingIntent.getActivity;
import static java.sql.Types.NULL;

public class PostActivity extends AppCompatActivity {

    private ImageButton msetImage;
    private EditText mPosttitle;
    private EditText mPostDesc;
    private Button submit;
    private Uri mimageUri=null;
    private StorageReference mstorage;
    private DatabaseReference mDatabase;
    private  ProgressDialog mprogress;

    private  static final int GALLERY_REQ=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);

        msetImage=(ImageButton)findViewById(R.id.imageSelect);
        mPosttitle=(EditText)findViewById(R.id.Title);
        mPostDesc=(EditText)findViewById(R.id.Description);
        submit=(Button)findViewById(R.id.submit);
        mstorage= FirebaseStorage.getInstance().getReference();
        mDatabase= FirebaseDatabase.getInstance().getReference().child("blog");
        mprogress=new ProgressDialog(this);
        msetImage.setClickable(false);
        msetImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent galleryintent=new Intent(Intent.ACTION_GET_CONTENT);
                msetImage.setClickable(true);
                galleryintent.setType("Image/*");
                startActivityForResult(galleryintent,GALLERY_REQ);

            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPosting();
            }
        });
    }

    private void startPosting()
    {
        mprogress.setMessage("Posting to Blog.....");
        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) {
                        Uri download=taskSnapshot.getDownloadUrl();
                    DatabaseReference newPost=mDatabase.push();
                    newPost.child("title").setValue(title_val);
                    newPost.child("Desc").setValue(desc_val);
                    newPost.child("image").setValue(download.toString());
                    mprogress.dismiss();

                }
            });
        }

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==GALLERY_REQ && resultCode==RESULT_OK){
              mimageUri  =data.getData();
            msetImage.setImageURI(mimageUri);

        }
    }

}

0 个答案:

没有答案