I need to call data from Firebase.

huangapple go评论73阅读模式
英文:

I need to call data from fireabse

问题

我没有遇到错误但我想要打印用户在上述活动中选择的值而且图像确实成功显示在Firebase存储中但它就是不会打印在图像视图中我该如何解决这个问题以下是我的代码

package tech.ahawebsolutions.ahachatsandcalling;

import ... //(省略导入部分,请查看您的原始代码)

public class SettingsActivity extends AppCompatActivity {

    //(省略部分代码,请查看您的原始代码)

    @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)
            {
                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())
                        {
                            //(省略部分代码,请查看您的原始代码)

                            RootRef.child("Users").child(currentUserID).child("image")
                                    .setValue(downloaedUrl)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task)
                                        {
                                            if (task.isSuccessful())
                                            {
                                                //(省略部分代码,请查看您的原始代码)

                                                Picasso.get().load(downloaedUrl).into(userProfileImage);
                                            }
                                            else
                                            {
                                                //(省略部分代码,请查看您的原始代码)
                                            }
                                        }
                                    });
                        }
                        else
                        {
                            //(省略部分代码,请查看您的原始代码)
                        }
                    }
                });
            }
        }
    }

    //(省略部分代码,请查看您的原始代码)
}

请注意,由于篇幅限制,我只翻译并返回了您提供的代码的一部分。如果您有任何关于解决问题的具体问题,请随时问我。

英文:

I am not getting an error but I want to print the value that the user chooses inside the activity above and the image does successfully show up in the firebase storage but it just won't print in the image view. How do I resolve this. Here is my code

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

package tech.ahawebsolutions.ahachatsandcalling;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import java.io.InputStream;
import java.util.HashMap;
public class SettingsActivity extends AppCompatActivity {
private Button UpdateAccountSettings, logoutbutton;
private EditText userName, userStatus;
private ImageView userProfileImage, userProfileImage1;
private String currentUserID;
private FirebaseAuth mAuth;
private DatabaseReference RootRef;
private StorageReference userProfileImagesRef;
private static final int GalleryPick = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
logoutbutton = (Button) findViewById(R.id.logoutbutton);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
RootRef = FirebaseDatabase.getInstance().getReference();
userProfileImagesRef = FirebaseStorage.getInstance().getReference().child(&quot;Profile Images&quot;);
userProfileImage = (ImageView) findViewById(R.id.set_profile_image);
userProfileImage1 = (ImageView) findViewById(R.id.set_profile_image1);
InitializeFields();
userName.setVisibility(View.INVISIBLE);
UpdateAccountSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
UpdateSettings();
}
});
RetrieveUserInfo();
logoutbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAuth.signOut();
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
}
});
userProfileImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType(&quot;image/*&quot;);
startActivityForResult(galleryIntent, GalleryPick);
}
});
}
private void UpdateSettings()
{
String setUserName = userName.getText().toString();
String setUserStatus = userStatus.getText().toString();
if (TextUtils.isEmpty(setUserName))
{
Toast.makeText(this, &quot;Please enter username&quot;, Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(setUserStatus))
{
Toast.makeText(this, &quot;Please enter your status&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;, setUserStatus);
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())
{
Toast.makeText(SettingsActivity.this, &quot;Profile updated successfully&quot;, Toast.LENGTH_SHORT).show();
SendUserToMainActivity();
}
else
{
String message = task.getException().toString();
Toast.makeText(SettingsActivity.this, &quot;Error :&quot; + message, Toast.LENGTH_SHORT).show();
}
}
});
}
}
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 = (ImageView) findViewById(R.id.set_profile_image);
}
@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)
{
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 uploaded Successfully...&quot;, Toast.LENGTH_SHORT).show();
final String downloaedUrl = task.getResult().getStorage().getDownloadUrl().toString();
RootRef.child(&quot;Users&quot;).child(currentUserID).child(&quot;image&quot;)
.setValue(downloaedUrl)
.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 save in Database, Successfully...&quot;, Toast.LENGTH_SHORT).show();
Picasso.get().load(downloaedUrl).into(userProfileImage);
}
else
{
String message = task.getException().toString();
Toast.makeText(SettingsActivity.this, &quot;Error: &quot; + message, 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() {
@Override
public void onDataChange(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 = dataSnapshot.child(&quot;name&quot;).getValue().toString();
String retrievesStatus = 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(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();
}
}

<!-- end snippet -->
The expected outcome would be the image the user selects and that is stored in firebase become called back and printed into the image view called userProfileImage. Thank you and if you can offer solutions that would be appreciated.

答案1

得分: 1

Sure, here's the translation:

findViewById(R.id.your_View_id) 这部分缺失了,尝试使用视图/数据绑定来避免这些问题。
英文:

findViewById(R.id.your_View_id) that was missing try to use view/data binding to avoid these problems

huangapple
  • 本文由 发表于 2020年8月14日 19:34:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63411974.html
匿名

发表评论

匿名网友

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

确定