Android Firebase - 向当前用户更新新图像会停止应用程序(不会崩溃)

时间:2017-02-10 02:03:57

标签: android firebase firebase-realtime-database firebase-storage

我试图通过点击图片浏览来更改个人资料图片,然后我就能裁剪图像然后存储到我的Firebase存储中。但是,添加新图像文件时,程序会停止并且无法单击任何内容。应用程序甚至没有崩溃也没有关闭。当我看到我的安卓监视器时,它会不停地打印出一些东西。我认为这会在某个地方进入某种无限循环。有人认出这个问题吗?

P.S。当我更新新的个人资料图片时,我发现我的Firebase数据库中我的用户下的image子项变化非常快,来回。如果先前的图像是“1”并且图像I更新是“2”,则它变得像1-> 2-> 1-> 2-> 1-> 2 ..就像疯了一样。他们非常快速地互相交换

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 String image;
    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)
            {
                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();

                image = dataSnapshot.child("image").getValue().toString();
                Picasso.with(MyProfileActivity.this).load(image).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();

                mProfileImage.setImageURI(mImageUri);

                filepath = mStorage.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Profile_Images").child(mImageUri.getLastPathSegment());

                filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>()
                {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
                    {
                        final Uri downloadUrl = taskSnapshot.getDownloadUrl();

                        Toast.makeText(MyProfileActivity.this, "You added picture successfully", Toast.LENGTH_SHORT).show();
                        mDatabaseUser.child(mUser.getUid()).addValueEventListener(new ValueEventListener()
                        {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot)
                            {
                                Toast.makeText(MyProfileActivity.this, "Changing picture now", Toast.LENGTH_SHORT).show();
                                DatabaseReference user_db = mDatabaseUser.child(mUser.getUid());
                                user_db.child("image").setValue(downloadUrl.toString());
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError)
                            {

                            }
                        });
                    }
                });
            }
            else if(resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
            {
                Exception error = result.getError();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的应用正在循环显示如下所示的代码。您向位置mDatabaseUser.child(mUser.getUid())添加了一个侦听器。当该位置的数据发生变化时,onDataChange()会运行并为位置setValue()的子image执行mDatabaseUser.child(mUser.getUid())。虽然您只是更改了该位置的子项,但它被视为对父位置的更改,导致侦听器再次触发,再次设置该值,等等,无休止地。

mDatabaseUser.child(mUser.getUid()).addValueEventListener(new ValueEventListener()
{
    @Override
    public void onDataChange(DataSnapshot dataSnapshot)
    {
        Toast.makeText(MyProfileActivity.this, "Changing picture now", Toast.LENGTH_SHORT).show();
        DatabaseReference user_db = mDatabaseUser.child(mUser.getUid());
        user_db.child("image").setValue(downloadUrl.toString());
    }

    @Override
    public void onCancelled(DatabaseError databaseError)
    {

    }
});

您在同一位置也有两个侦听器,一个在上面显示,另一个在onCreate()方法中。当位置的任何部分发生变化时,两个听众都会触发。