感謝您的閱讀。我正在研究MyAccountActivity,用戶將可以更改他們的個人資料圖片。我在這裏做的是當在onActivityResult()方法中完成裁剪圖像時,程序將更改當前用戶的圖像值。當圖像值發生變化時,onCreate()方法中的addValueEventListener會再次出現以讀取該值並將該圖像url或字符串存儲到Picasso中,就像我在addValueEventListener中那樣。但是,應用程序會在裁剪後自動將我簽名。Android Firebase - 用戶在更改子數據後自動註銷
通過打印出mImageUrl,我注意到當我成功裁剪了我的圖像後,它改變了它的值,但是一秒鐘之後它又回到上一張圖片,然後它將我註銷。
任何人都可以幫助我嗎?謝謝。
MyProfileActivity.java
public class MyProfileActivity extends AppCompatActivity
{
/*UI*/
private Toolbar mMyProfileToolbar;
private TextView mAge;
private TextView mGender;
private TextView mReligion;
private TextView mSports;
private TextView mState;
private TextView mTimeRegistered;
private TextView mDisplayName;
private CircleImageView mProfileImage;
/*Firebase*/
private DatabaseReference mDatabaseUser;
private FirebaseUser mUser;
private StorageReference mStorage;
private DatabaseReference mDatabasePost;
private StorageReference filepath;
private DatabaseReference user_db;
/*Other*/
private String age;
private String gender;
private String religion;
private String sports;
private String state;
private String timeRegistered;
private String displayName;
private Uri mImageUri = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_profile);
/*UI*/
mMyProfileToolbar = (Toolbar)findViewById(R.id.activity_my_profile_toolbar);
mProfileImage = (CircleImageView)findViewById(R.id.activity_my_profile_profile_image);
mDisplayName = (TextView)findViewById(R.id.activity_my_profile_display_name);
mAge = (TextView)findViewById(R.id.activity_my_profile_age);
mGender = (TextView)findViewById(R.id.activity_my_profile_gender);
mReligion = (TextView)findViewById(R.id.activity_my_profile_religion);
mSports = (TextView)findViewById(R.id.activity_my_profile_sports);
mState = (TextView)findViewById(R.id.activity_my_profile_state);
mTimeRegistered = (TextView)findViewById(R.id.activity_my_profile_time_registered);
/*Firebase*/
mDatabaseUser = FirebaseDatabase.getInstance().getReference().child("User");
mUser = FirebaseAuth.getInstance().getCurrentUser();
mStorage = FirebaseStorage.getInstance().getReference();
/*Function*/
setSupportActionBar(mMyProfileToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(null);
mDatabaseUser.child(mUser.getUid()).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
System.out.println("TestUp------------------------------------at addValueEventListener");
displayName = dataSnapshot.child("displayName").getValue().toString();
age = dataSnapshot.child("age").getValue().toString();
gender = dataSnapshot.child("gender").getValue().toString();
religion = dataSnapshot.child("religion").getValue().toString();
sports = dataSnapshot.child("sports").getValue().toString();
state = dataSnapshot.child("state").getValue().toString();
timeRegistered = dataSnapshot.child("timeRegistered").getValue().toString();
mImageUri = Uri.parse(dataSnapshot.child("image").getValue().toString());
System.out.println("TestUp------------------------------------mImageUri: " + mImageUri);
Picasso.with(MyProfileActivity.this).load(mImageUri).into(mProfileImage);
mDisplayName.setText(displayName);
mAge.setText("Age: " + age);
mGender.setText("Gender: " + gender);
mReligion.setText("Religion: " + religion);
mSports.setText("Sports: " + sports);
mState.setText("State: " + state);
mTimeRegistered.setText("Time Registered: " + timeRegistered);
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
});
mProfileImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("image/*");
startActivityForResult(i, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK)
{
mImageUri = data.getData();
CropImage.activity(mImageUri)
.setGuidelines(CropImageView.Guidelines.OFF)
.setAspectRatio(1, 1)
.start(this);
}
if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
{
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK)
{
mImageUri = result.getUri();
DatabaseReference user_db = mDatabaseUser.child(mUser.getUid());
user_db.child("image").setValue(mImageUri.toString());
System.out.println("TestDown------------------------------------user_db: " + user_db);
System.out.println("TestDown------------------------------------mImageUri: " + mImageUri);
}
else if(resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
{
System.out.println("Test9------------------------------------");
Toast.makeText(MyProfileActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
Exception error = result.getError();
System.out.println("Test10------------------------------------");
}
}
}
}
規則火力地堡實時數據庫
{
"rules": {
// User profiles are only readable/writable by the user who owns it
".read": true,
".write": true,
"users": {
"$UID": {
".read": "auth.uid == $UID",
".write": "auth.uid == $UID"
}
},
// Posts can be read by anyone but only written by logged-in users.
"posts": {
".read": true,
".write": "auth.uid != null",
"$POSTID": {
// UID must matched logged in user and is fixed once set
"uid": {
".validate": "(data.exists() && data.val() == newData.val()) || newData.val() == auth.uid"
},
// User can only update own stars
"stars": {
"$UID": {
".validate": "auth.uid == $UID"
}
}
}
},
// User posts can be read by anyone but only written by the user that owns it,
// and with a matching UID
"user-posts": {
".read": true,
"$UID": {
"$POSTID": {
".write": "auth.uid == $UID",
".validate": "data.exists() || newData.child('uid').val() == auth.uid"
}
}
},
// Comments can be read by anyone but only written by a logged in user
"post-comments": {
".read": true,
".write": "auth.uid != null",
"$POSTID": {
"$COMMENTID": {
// UID must matched logged in user and is fixed once set
"uid": {
".validate": "(data.exists() && data.val() == newData.val()) || newData.val() == auth.uid"
}
}
}
}
}
}
它仍然簽署我.. – Eric
當我註釋掉user_db.child( 「圖像」)的setValue(mImageUri.toString())。 ,它不會讓我出去。我認爲user_db.child(「image」)有問題。setValue(mImageUri.toString()); – Eric
您的設定值代碼沒有問題。問題是你的mDatabaseUser.child(mUser.getUid())有一個監聽器,當你改變它的任何事情時,監聽器會觸發。所以,這是關於你的監聽器和addListenerForSingleValueEvent應該工作。 mDatabaseUser.child(mUser.getUid())上還有其他活動偵聽器嗎? – kagkar