我正在关注Code for sending images using firebase
我已经配置了firebase存储url,数据库url,数据库规则(true),sha1在app文件夹中放置了google-services.json,但是当我调试代码时,它(用户)显示空值FirebaseUser user = firebaseAuth.getCurrentUser();
我的代码如下:
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import butterknife.Bind;
import butterknife.ButterKnife;
public class LoginActivity
extends AppCompatActivity
implements FirebaseAuth.AuthStateListener
{
@Bind( R.id.login_button )
Button loginButton;
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
@Override
protected void onCreate( @Nullable Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_login );
ButterKnife.bind( this );
}
@Override
public void onStart()
{
super.onStart();
firebaseAuth.addAuthStateListener( this );
verifyCurrentUserAndStartMainActivityIfLoggedIn();
}
@Override
public void onStop()
{
super.onStop();
firebaseAuth.removeAuthStateListener( this );
}
public void onLoginClicked( View view )
{
loginButton.setEnabled( false );
firebaseAuth.signInAnonymously();
}
@Override
public void onAuthStateChanged( @NonNull FirebaseAuth firebaseAuth )
{
verifyCurrentUserAndStartMainActivityIfLoggedIn();
}
private void verifyCurrentUserAndStartMainActivityIfLoggedIn()
{
FirebaseUser user = firebaseAuth.getCurrentUser();
if ( user != null ) //HERE I AM GETTING "user=null" ??
{
startActivity( new Intent( this, MainActivity.class ) );
Toast.makeText(this,"Login success",Toast.LENGTH_LONG).show();
finish();
}
else
{
loginButton.setEnabled( true );
}
}
}
成功登录后(user!= null)下一个活动现在显示下一个活动的样本网址
// StorageReference storageRef = storage.getReferenceFromUrl( "gs://funchat-ef3ed.appspot.com" );
我在firebase中创建了新存储,获取的URL是:
StorageReference storageRef = storage.getReferenceFromUrl( "gs://fir-imagesending-1a8c1.appspot.com");
当我使用上面的url应用程序崩溃时。代码如下:
public class MainActivity extends AppCompatActivity
{
static final int REQUEST_IMAGE_CAPTURE = 1;
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
DatabaseReference databaseReference = database.getReference( "messages" );
FirebaseStorage storage = FirebaseStorage.getInstance();
// StorageReference storageRef = storage.getReferenceFromUrl( "gs://funchat-ef3ed.appspot.com" );
StorageReference storageRef = storage.getReferenceFromUrl( "gs://fir-imagesending-1a8c1.appspot.com");
我无法找到我失踪的地方?
答案 0 :(得分:1)
您的代码似乎远非官方firebase documentation
mAuth.signInAnonymously().addOnCompleteListener
onComplete()
中观察Task.isSuccessful()
是否为真,如果不是task.getException()
则返回。如果您真的想知道FirebaseUser
为null
的原因,请阅读firebase docs
如果您发布异常,我可以帮助您。
答案 1 :(得分:0)
如果您没有,则必须允许匿名登录,然后signInAnonymously()...
或者您可以使用firebase提供的google登录进行用户登录(首先允许用户在firebase控制台中使用google登录),然后将所有实例存储为单例类。 很容易做到安全性。