Reputation: 432
I created an activity from which I can add product title,description,price and image to the database, but when I click on submit button nothing happens. I've given internet permission, enabled Firebase authentication (Email/Password and Google).
I can't find any error, in logcat I found these, but I don't know how to resolve:
DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@f09391f
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
Also the database rules is :
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
and Storage rules are:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
My Activity Code is:
public class createPost extends AppCompatActivity {
private ImageButton m_SelectImage;
private EditText mProdTitle;
private EditText mProdDesc;
private EditText mProdPrice;
private Button mSubmit;
private static final int GALLERY_REQUEST = 1;
private Uri mImageUri = null;
private StorageReference mStorage;
private DatabaseReference mDatabase;
private ProgressDialog mProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_post);
m_SelectImage = (ImageButton)findViewById(R.id.p_image);
mProdTitle = (EditText)findViewById(R.id.p_title);
mProdDesc = (EditText)findViewById(R.id.p_description);
mProdPrice = (EditText)findViewById(R.id.p_price);
mSubmit = (Button)findViewById(R.id.p_submit);
mStorage = FirebaseStorage.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Products");
mProgress = new ProgressDialog(this);
m_SelectImage.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);
}
});
mSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startPosting();
}
});
}
private void startPosting() {
mProgress.setMessage("Adding new Post");
final String title_val = mProdTitle.getText().toString().trim();
final String desc_val= mProdDesc.getText().toString().trim();
final String price_val = mProdPrice.getText().toString().trim();
if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && TextUtils.isEmpty(price_val) && mImageUri!=null)
{
mProgress.show();
StorageReference filePath = mStorage.child("Products").child(mImageUri.getLastPathSegment());
filePath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadURL = taskSnapshot.getDownloadUrl();
DatabaseReference newPost = mDatabase.push();
newPost.child("Title").setValue(title_val);
newPost.child("Description").setValue(desc_val);
newPost.child("Price").setValue(price_val);
newPost.child("Product_Image").setValue(downloadURL.toString());
mProgress.dismiss();
Toast.makeText(createPost.this,"Posting Complete",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(createPost.this,"Error Occured"+e.getMessage(),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)
{
mImageUri=data.getData();
m_SelectImage.setImageURI(mImageUri);
}
}
}
Also, I can't find any error in my code (if any).
Upvotes: 0
Views: 1221
Reputation: 733
I found your mistake. A missing ! in your condition. Change this
if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && TextUtils.isEmpty(price_val) && mImageUri!=null)
to this:
if (!TextUtils.isEmpty(title_val) && !TextUtils.isEmpty(desc_val) && !TextUtils.isEmpty(price_val) && mImageUri!=null)
Upvotes: 1
Reputation: 733
Is there a user signed in, when you click on submit button? You can check that by adding following code to onCreate():
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Log.d(getSubClassTAG(), "onAuthStateChanged: singed in: " + user.getUid());
} else {
Log.d(getSubClassTAG(), "onAuthStateChanged: signed out: ");
}
}
};
If not, you have to sign in a user like following:
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmailAndPasswort: onComplete: " + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmailAndPassword: failed ", task.getException());
} else {
//signin was successful
}
}
});
Upvotes: 1