我是android studio的初学者。我正在尝试保存用户在firebase数据库和存储中写入的图像,标题,帖子。
由于我没有进行注册页面,因此我将实时数据库中的规则设置为== true,以便读取和写入.also request.auth == true in storage rules。
但它没有保存在数据库中。进步对话永远不会被驳回。 当我编写一个Toast进行检查时,我知道问题在于onSuccess方法。
日志显示一些身份验证错误。
请告诉我应该在代码中进行哪些更正?
package com.awani.simpleblogapp;
import android.app.ProgressDialog;
import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
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 static android.widget.Toast.*;
public class PostActivity extends AppCompatActivity {
private ImageButton setImage;
private static final int GALLERY_REQUEST = 1;
private Button submitButton;
private EditText titleField;
private EditText postField;
private Uri imageUri = null;
private StorageReference mStorageRef;
private DatabaseReference mDatabase;
private ProgressDialog progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
setImage = (ImageButton) findViewById(R.id.setImage);
submitButton=(Button)findViewById(R.id.submitButton);
titleField=(EditText)findViewById(R.id.titleField);
postField=(EditText)findViewById(R.id.postField);
mStorageRef = FirebaseStorage.getInstance().getReference(); //this directs us to the root director of the storage
mDatabase = FirebaseDatabase.getInstance().getReference().child("blog");
progressbar = new ProgressDialog(this);
setImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startPosting();
}
});
}
private void startPosting() {
progressbar.setMessage("Posting to the blog.... ");
progressbar.show();
final String title_val = titleField.getText().toString();
final String post_val=postField.getText().toString();
if(!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(post_val) && imageUri!=null){
StorageReference filepath = mStorageRef.child("Blog_Images").child(imageUri.getLastPathSegment());
filepath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();//provides us download Uri
DatabaseReference newPost = mDatabase.push();
newPost.child("title").setValue(title_val);
newPost.child("Post").setValue(post_val);
newPost.child("image").setValue(downloadUri.toString());
progressbar.dismiss();;
//make new intent to direct the uer after submiting
// startActivity(new Intent(PostActivity.this,MainActivity.class));
}
});
}
}
@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();
setImage.setImageURI(imageUri);
}
}
}
答案 0 :(得分:0)
我曾经做过类似的事情..请使用此代码,随意根据自己的喜好编辑它,但到目前为止,代码正在运行
import android.app.Notification;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuthException;
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.net.URI;
public class PostActivity4Home extends AppCompatActivity {
private ImageButton mSelectImage;
private EditText mPostTitle;
private EditText mPostDesc;
private Button mSubmitBtn;
private Uri mImageUri = null;
private static final int GALLERY_REQUEST = 1;
private StorageReference mStorage;
private DatabaseReference mDatabase;
private ProgressDialog mProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_activity4_home);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Home");
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 view) {
startPosting();
}
});
}
private void startPosting() {
mProgress.setMessage("Posting...");
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("HomePostImages").child(mImageUri.getLastPathSegment());
filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
mProgress.dismiss();
DatabaseReference newPost = mDatabase.push();
newPost.child("Title").setValue(title_val);
newPost.child("Desc").setValue(desc_val);
newPost.child("Image").setValue(downloadUrl.toString());
startActivity(new Intent(PostActivity4Home.this, MainActivity.class));
}
});
}
}
@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);
}
}