Profile image not showing after saving it, it just shows a white blank screen

huangapple 未分类评论44阅读模式
英文:

Profile image not showing after saving it, it just shows a white blank screen

问题

以下是您提供的代码的翻译部分:

当我保存完我的个人资料图片后它显示图片已更新但当我再次打开设置时没有显示任何图片而是一个白屏虽然它显示了状态

这是我的代码

private Button UpdateAccountSettings;
private EditText userName, userStatus;
private CircleImageView userProfileImage;

private String currentUserID;
private FirebaseAuth mAuth;
private DatabaseReference Rootref;
private static final int GalleryPick = 1;
private StorageReference UserProfileImagesRef;

private ProgressDialog loadingBar;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    mAuth = FirebaseAuth.getInstance();
    currentUserID = mAuth.getCurrentUser().getUid();
    Rootref = FirebaseDatabase.getInstance().getReference();

    UserProfileImagesRef= FirebaseStorage.getInstance().getReference().child("Profile Images");

    InitializeFields();

    userName.setVisibility(View.INVISIBLE);

    UpdateAccountSettings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            UpdateSettings();
        }
    });

    RetrieveUserInfo();

    userProfileImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Intent galleryIntent = new Intent();
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent, GalleryPick);
        }
    });
}

private void InitializeFields() {
    UpdateAccountSettings = (Button) findViewById(R.id.update_settings_button);
    userName = (EditText) findViewById(R.id.set_user_name);
    userStatus = (EditText) findViewById(R.id.set_profile_status );
    userProfileImage= (CircleImageView) findViewById(R.id.set_profile_image );
    loadingBar = new ProgressDialog(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GalleryPick && resultCode == RESULT_OK && data != null)
    {
        Uri ImageUri = data.getData();

        CropImage.activity()
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(1, 1)
                .start(this);
    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK)
        {
            loadingBar.setTitle("设置个人资料图片");
            loadingBar.setMessage("请等待,正在更新您的个人资料图片...");
            loadingBar.setCanceledOnTouchOutside(false);
            loadingBar.show();

            Uri resultUri =  result.getUri();
            StorageReference filePath = UserProfileImagesRef.child(currentUserID + ".jpg");

            filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if (task.isSuccessful())
                    {
                        Toast.makeText(SettingsActivity.this, "个人资料图片上传成功...", Toast.LENGTH_SHORT).show();

                        final String downloadedUrl = task.getResult().getStorage().getDownloadUrl().toString();

                        Rootref.child("Users").child(currentUserID).child("image")
                                .setValue(downloadedUrl)
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {

                                        if (task.isSuccessful())
                                        {
                                            Toast.makeText(SettingsActivity.this, "图片已成功保存至数据库...", Toast.LENGTH_SHORT).show();
                                            loadingBar.dismiss();
                                        }
                                        else
                                        {
                                            String message = task.getException().toString();
                                            Toast.makeText(SettingsActivity.this, "", Toast.LENGTH_SHORT).show();
                                            loadingBar.dismiss();
                                        }
                                    }
                                });
                    }
                    else
                    {
                        String message = task.getException().toString();
                        Toast.makeText(SettingsActivity.this, "错误:" + message, Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();
                    }
                }
            });
        }
    }
}

// 其他方法的翻译...

请注意,为了保持您的代码的结构完整性,我已经翻译了部分内容,但没有一一翻译每一行代码。如果您有特定的问题或需要进一步的帮助,请随时提问。

英文:

After I save my profile image it says image updated but when I open settings again it does not show any image; instead, a white screen. It shows the status though.

Here is my code:

private Button UpdateAccountSettings;
private EditText userName, userStatus;
private CircleImageView userProfileImage;

private String currentUserID;
private FirebaseAuth mAuth;
private DatabaseReference Rootref;
private static final int GalleryPick = 1;
private StorageReference UserProfileImagesRef;

private ProgressDialog loadingBar;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    mAuth = FirebaseAuth.getInstance();
    currentUserID = mAuth.getCurrentUser().getUid();
    Rootref = FirebaseDatabase.getInstance().getReference();

    UserProfileImagesRef= FirebaseStorage.getInstance().getReference().child(&quot;Profile Images&quot;);

    InitializeFields();

    userName.setVisibility(View.INVISIBLE);

    UpdateAccountSettings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            UpdateSettings();
        }
    });


    RetrieveUserInfo();

    userProfileImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Intent galleryIntent = new Intent();
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType(&quot;image/*&quot;);
            startActivityForResult(galleryIntent,GalleryPick);
        }
    });

}



private void InitializeFields() {
    UpdateAccountSettings = (Button) findViewById(R.id.update_settings_button);
    userName = (EditText) findViewById(R.id.set_user_name);
    userStatus = (EditText) findViewById(R.id.set_profile_status );
    userProfileImage= (CircleImageView) findViewById(R.id.set_profile_image );
    loadingBar = new ProgressDialog(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode== GalleryPick &amp;&amp; resultCode== RESULT_OK &amp;&amp; data!= null)
    {
        Uri ImageUri = data.getData();

        CropImage.activity()
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(1,1)
                .start(this);
    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK)
        {
            loadingBar.setTitle(&quot;set Profile Image&quot;);
            loadingBar.setMessage(&quot;Please wait, your profile image is updating...&quot;);
            loadingBar.setCanceledOnTouchOutside(false);
            loadingBar.show();

            Uri resultUri =  result.getUri();
            StorageReference filePath = UserProfileImagesRef.child(currentUserID + &quot;.jpg&quot;);

            filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener&lt;UploadTask.TaskSnapshot&gt;() {
                @Override
                public void onComplete(@NonNull Task&lt;UploadTask.TaskSnapshot&gt; task) {
                   if (task.isSuccessful())
                   {
                       Toast.makeText(SettingsActivity.this,&quot; Profile Image Uploded succesfully...&quot;, Toast.LENGTH_SHORT).show();


                       final String downloadedUrl = task.getResult().getStorage().getDownloadUrl().toString();

                       Rootref.child(&quot;Users&quot;).child(currentUserID).child(&quot;image&quot;)
                               .setValue(downloadedUrl)
                               .addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                                   @Override
                                   public void onComplete(@NonNull Task&lt;Void&gt; task) {

                                       if (task.isSuccessful())
                                       {
                                           Toast.makeText(SettingsActivity.this,&quot;Image saved in database Successfully...&quot;, Toast.LENGTH_SHORT).show();

                                           loadingBar.dismiss();

                                       }
                                       else
                                       {
                                           String message = task.getException().toString();
                                           Toast.makeText(SettingsActivity.this,&quot;&quot;, Toast.LENGTH_SHORT).show();

                                           loadingBar.dismiss();
                                       }
                                   }
                               });
                   }
                   else
                   {
                       String message = task.getException().toString();
                       Toast.makeText(SettingsActivity.this,&quot;Error: &quot; + message, Toast.LENGTH_SHORT).show();

                       loadingBar.dismiss();
                   }


                }
            });

        }

    }
}

private void UpdateSettings() {

    String setUserName = userName.getText().toString();
    String setStatus = userStatus.getText().toString();

    if(TextUtils.isEmpty(setUserName)){

        Toast.makeText(this,&quot;Please write your username first..&quot;, Toast.LENGTH_SHORT).show();

    }

    if(TextUtils.isEmpty(setStatus)){

        Toast.makeText(this,&quot;Please write your Status first..&quot;, Toast.LENGTH_SHORT).show();

    }
    else {
        HashMap&lt;String, String&gt; profileMap = new HashMap&lt;&gt;();
        profileMap.put(&quot;uid&quot;, currentUserID);
        profileMap.put(&quot;name&quot;, setUserName);
        profileMap.put(&quot;Status&quot;, setStatus);

        Rootref.child(&quot;Users&quot;).child(currentUserID).setValue( profileMap)
                .addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                    @Override
                    public void onComplete(@NonNull Task&lt;Void&gt; task) {
                        if(task.isSuccessful()){
                            SendUserToMainActivity();

                            Toast.makeText(SettingsActivity.this, &quot;Profile  Updated Succesfully...&quot;, Toast.LENGTH_SHORT).show();


                        }
                        else {
                            String message = task.getException().toString();
                            Toast.makeText(SettingsActivity.this,    &quot;Error:&quot; + message, Toast.LENGTH_SHORT).show();

                        }


                    }
                });
    }
}

private void RetrieveUserInfo()
{
    Rootref.child(&quot;Users&quot;).child(currentUserID)
            .addValueEventListener(new ValueEventListener() {
                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                {
                    if((dataSnapshot.exists()) &amp;&amp; (dataSnapshot.hasChild(&quot;name&quot;) ) &amp;&amp;(dataSnapshot.hasChild(&quot;image&quot;)))

                    {
                        String retrieveUserName = dataSnapshot.child(&quot;name&quot;).getValue().toString();
                        String retrievesStatus = dataSnapshot.child(&quot;Status&quot;).getValue().toString();
                        String retrieveProfileImage = dataSnapshot.child(&quot;image&quot;).getValue().toString();

                        userName.setText(retrieveUserName);
                        userStatus.setText(retrievesStatus);
                        Picasso.get().load(retrieveProfileImage).into(userProfileImage);

                    }
                    else if((dataSnapshot.exists()) &amp;&amp; (dataSnapshot.hasChild(&quot;name&quot;)))
                    {
                        String retrieveUserName = Objects.requireNonNull(dataSnapshot.child(&quot;name&quot;).getValue()).toString();
                        String retrievesStatus = Objects.requireNonNull(dataSnapshot.child(&quot;Status&quot;).getValue()).toString();


                        userName.setText(retrieveUserName);
                        userStatus.setText(retrievesStatus);
                    }
                    else{

                        userName.setVisibility(View.VISIBLE);
                        Toast.makeText(SettingsActivity.this, &quot;Please set &amp; update your profile information...&quot;, Toast.LENGTH_SHORT).show();
                    }

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });


}



private void SendUserToMainActivity() {
    Intent mainIntent = new Intent(SettingsActivity.this, MainActivity.class);
    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(mainIntent);
    finish();
}
}

My logcat does not show any error, please help me.

huangapple
  • 本文由 发表于 2020年7月27日 06:52:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/63106585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定