这是一个奇怪的firebase问题。我将数据发布到firebase,有时它会在同一时间发布,有时则不会在firebase上发布数据。这真的很奇怪,我无法理解为什么会这样。 当我从Android工作室运行应用程序时,它在我在应用程序中发布的同时发布数据但是在一段时间后它表现得很奇怪我无法发布数据。
package com.example.hp.heartful;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
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 com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
public class NewsPost extends AppCompatActivity {
private ImageButton userImage;
private EditText title,userDesc;
private Button submitbtn;
private DatabaseReference mdatabase;
private Uri imageUri=null;
private ProgressDialog progress;
private StorageReference newsPhotos;
private FirebaseStorage mstorage;
private final static int GALLERY_REQUEST=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_post);
// FirebaseDatabase.getInstance().setPersistenceEnabled(true);
mstorage=FirebaseStorage.getInstance();
mdatabase= FirebaseDatabase.getInstance().getReference().child("News");
newsPhotos=mstorage.getReference().child("NewsImages");
submitbtn=(Button)findViewById(R.id.submit_button);
title=(EditText)findViewById(R.id.title);
userDesc=(EditText)findViewById(R.id.user_des);
progress=new ProgressDialog(this);
userImage=(ImageButton)findViewById(R.id.user_image);
userImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gallery=new Intent(Intent.ACTION_GET_CONTENT);
gallery.setType("image/jpeg");
gallery.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(gallery, "Complete action using"),GALLERY_REQUEST);
}
});
submitbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progress.setMessage("News Posting, please wait...");
userPost();
}
});
}
private void userPost(){
final String postTitle=title.getText().toString();
final String postDes= userDesc.getText().toString();
if (!TextUtils.isEmpty(postTitle)&&!TextUtils.isEmpty(postDes)&&imageUri!=null){
progress.show();
StorageReference filePath=newsPhotos.child(imageUri.getLastPathSegment());
filePath.putFile(imageUri).addOnSuccessListener(this,new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
@SuppressWarnings("VisibleForTests")
Uri downloadUrl = taskSnapshot.getDownloadUrl();
DatabaseReference newPost=mdatabase.push();
newPost.child("Title").setValue(postTitle);
newPost.child("Description").setValue(postDes);
newPost.child("Image").setValue(downloadUrl.toString());
progress.dismiss();
startActivity(new Intent(NewsPost.this,FragmentTwo.class));
}
});
}
else{
Toast.makeText(NewsPost.this,"Please fill all the blanks",Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==GALLERY_REQUEST&&resultCode==RESULT_OK){
imageUri=data.getData();
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
imageUri=resultUri;
userImage.setImageURI(imageUri);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
Toast.makeText(NewsPost.this, (CharSequence) error,Toast.LENGTH_LONG).show();
}
}
}
}
这是新邮件活动
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.hp.heartful.NewsPost">
<ImageButton
android:layout_width="wrap_content"
android:id="@+id/user_image"
android:layout_margin="16dp"
android:layout_height="wrap_content"
android:src="@mipmap/add_btn"/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="Title..."
android:inputType="textMultiLine"
android:paddingLeft="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/text_field_design"
android:id="@+id/title"
android:layout_marginBottom="16dp"
android:layout_below="@+id/user_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:hint="Descriptions..."
android:inputType="textMultiLine"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:paddingLeft="16dp"
android:background="@drawable/text_field_design"
android:id="@+id/user_des"
android:layout_below="@+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:text="Submit"
android:textColor="#FFF"
android:textSize="24dp"
android:layout_height="wrap_content"
android:background="@drawable/custom_button1"
android:id="@+id/submit_button"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>