簡體   English   中英

當我嘗試從我的 Firebase 存儲中設置圖像時,我的 ImageView 消失了

[英]My ImageView disappears when I try to set the image from my Firebase storage

我想從我的存儲中設置一張 Picasso 的圖片,但是它不起作用。 最初,我的 ImageView 中有一個圖標,但進入 Activity 后它並沒有顯示。

這是代碼

public class ProfileFragment extends Fragment {

    TextView fullNameText, emailText, dateBirthText, phoneText;
    TextView addressText;
    FloatingActionButton profileEditBtn;
    TextView uploadImgBtn;
    ImageView profileImg;

    String phone, link;

    private DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference().child("image");


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle bundle = this.getArguments();
        phone = bundle.getString("phone");

        View myView = inflater.inflate(R.layout.fragment_profile, container, false);

        fullNameText = myView.findViewById(R.id.full_name_text);
        emailText = myView.findViewById(R.id.email_text);
        dateBirthText = myView.findViewById(R.id.date_birth_text);
        addressText = myView.findViewById(R.id.address_text);
        phoneText = myView.findViewById(R.id.phone_text);
        profileImg = myView.findViewById(R.id.profile_img);


        uploadImgBtn = myView.findViewById(R.id.upload_img_btn);
        uploadImgBtn.setOnClickListener(view -> {
            Intent intent = new Intent(getContext(), UploadProfileImage.class);
            intent.putExtra("phone", phone);
            startActivity(intent);
        });

        return myView;
    }

    @Override
    public void onStart() {
        super.onStart();

        dbRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                link = dataSnapshot.getValue(String.class);
                // this is the link to the image in firebase
                System.out.println(link);

                Picasso.get()
                        .load(link)
                        .fit()
                        .centerCrop()
                        .into(profileImg);

                profileImg.setVisibility(View.VISIBLE);
            }

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

            }
        });
    }
}

我的 xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/profile_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="My personal data"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.434" />

    <TextView
        android:id="@+id/full_name_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="Full Name: "
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/profile_title"
        app:layout_constraintVertical_bias="0.053" />

    <TextView
        android:id="@+id/email_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="Email: "
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/profile_title"
        app:layout_constraintVertical_bias="0.175" />

    <TextView
        android:id="@+id/date_birth_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="Date of birth: "
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/profile_title"
        app:layout_constraintVertical_bias="0.416" />

    <TextView
        android:id="@+id/address_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="Address: "
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/profile_title"
        app:layout_constraintVertical_bias="0.527" />

    <TextView
        android:id="@+id/phone_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="Phone: "
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/profile_title"
        app:layout_constraintVertical_bias="0.293" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/profile_edit_btn"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:clickable="true"
        android:contentDescription="Edit profile"
        app:fabCustomSize="64dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.953"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/address_text"
        app:layout_constraintVertical_bias="0.198"
        app:srcCompat="@drawable/ic_edit" />

    <TextView
        android:id="@+id/upload_img_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Upload profile image"
        android:textColor="#03A9F4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/profile_edit_btn"
        app:layout_constraintHorizontal_bias="0.079"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/address_text"
        app:layout_constraintVertical_bias="0.288" />

    <ImageView
        android:id="@+id/profile_img"
        android:layout_width="230dp"
        android:layout_height="230dp"
        app:layout_constraintBottom_toTopOf="@+id/profile_title"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.702"
        tools:ignore="ImageContrastCheck"
        android:src="@drawable/ic_android"/>


</androidx.constraintlayout.widget.ConstraintLayout>

我試過用 Glide 代替 picasso,但不管怎樣都沒用。 我也很確定我的文件鏈接是正確的。

實時數據庫

{
  "Users" : {
    "123456789" : {
      "address" : "",
      "date" : "2022-03-27",
      "email" : "admin@gmail.com",
      "fullName" : "me",
      "password" : "qwerty12345",
      "phone" : "123456789"
    }
  },
  "image" : "gs://myproj-8984f.appspot.com/images/gachiava.png"
}

請檢查畢加索是否可以通過手動添加url來加載另一個url。

Picasso.get().load("your link here").fit().centerCrop().into(profileImg);

並確保您已將正確的 inte.net 權限添加到 Android 清單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM