Firestore Recycler适配器-来自Firestore的图像

时间:2019-06-24 19:02:06

标签: android firebase android-recyclerview

我有我的聊天应用程序的回收者视图设置。我想添加它,以便用户能够在聊天中发送图片。我添加了图像视图,并添加了将图像上传到Firestore的代码。我很困惑的地方是实现代码以在回收站视图中显示它。

型号:

    package com.example.android.debateapp.Message;


public class Message {

    private String ChatMessage;
    private String mChatImageURL;


    public Message(){
        //Empty constructor needed
    }

    public Message(String ChatMessage, String mChatImageURL ){
        this.ChatMessage = ChatMessage;
        this.mChatImageURL = mChatImageURL;
    }

    public String getChatMessage() {
        return ChatMessage;
    }

    public String getmChatImageURL() {

        return mChatImageURL;
    }

}

MessageRecyclerAdapter:

public class MessageRecyclerAdapter extends FirestoreRecyclerAdapter<Message, MessageRecyclerAdapter.MessageHolder> {


public MessageRecyclerAdapter(@NonNull FirestoreRecyclerOptions<Message> options) {
    super(options);

}

@Override
protected void onBindViewHolder(@NonNull MessageHolder holder, int position, @NonNull Message model) {

    holder.ChatMessage.setText(model.getChatMessage());


}

@NonNull
@Override
public MessageHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.message_item, viewGroup, false);

    return new MessageHolder(v);
}

class MessageHolder extends RecyclerView.ViewHolder{

    TextView ChatMessage;
    CircleImageView MessageProfilePicture;
    ImageView ChatMessageImage;



    public MessageHolder(@NonNull View itemView) {
        super(itemView);

        ChatMessage = itemView.findViewById(R.id.Message_Chat_Content);
        MessageProfilePicture = itemView.findViewById(R.id.Chat_Profile_picture);
        ChatMessageImage = itemView.findViewById(R.id.chat_Message_image);

    }

    public void setChatImage(final String downloadUri) {

        ChatMessageImage = itemView.findViewById(R.id.chat_Message_image);
        Glide.with(itemView.getContext()).load(downloadUri).into(ChatMessageImage);

        }
    }
}

ChatActivity

public class ChatActivity extends AppCompatActivity {

public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000;
private static final int GALLERY_PICK = 1;

private ListView mMessageListView;
private ImageButton mPhotoPickerButton;
private EditText mMessageEditText;
private Button mSendButton;
private String mUsername;
private ChildEventListener mChildEventListner;
private ValueEventListener mValueEventListner;
private FirebaseUser mCurrentUser;

private StorageReference storageReference;

private StorageReference mChatPhotosStorageReference;
private Context mContext;

private MessageRecyclerAdapter adapter;

private ImageView chatimage;

private String TESTmessagedoc;

private ImageView chatProfile;


//FIRE STORE
private DocumentReference mMessageDoc;
private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
private CollectionReference collectionReference = (firebaseFirestore).collection("Messages");
private CollectionReference colRef;

private static final String TAG = ChatActivity.class.getName();


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

    storageReference = FirebaseStorage.getInstance().getReference();

    chatProfile = findViewById(R.id.Chat_Profile_picture);

    //chatimage = findViewById(R.id.photoImageView);

    final String messageDoc = getIntent().getStringExtra("Message_ID");

    TESTmessagedoc = messageDoc;

    mMessageDoc = firebaseFirestore.collection("Messages").document(messageDoc);


    colRef = firebaseFirestore.collection("Messages").document(messageDoc).collection("chats");

    mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
    mMessageEditText = (EditText) findViewById(R.id.messageEditText);
    mSendButton = (Button) findViewById(R.id.sendButton);

    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
    final String current_uid = mCurrentUser.getUid();

    // ImagePickerButton shows an image picker to upload a image for a message
    mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(galleryIntent, "Select Image"), GALLERY_PICK);
        }
    });


    setUpRecyclerView();

    // Enable Send button when there's text to send
    mMessageEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (charSequence.toString().trim().length() > 0) {
                mSendButton.setEnabled(true);
            } else {
                mSendButton.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)});

    mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String MessageText = mMessageEditText.getText().toString();


            Map<String, Object> chatMap = new HashMap<>();
            chatMap.put("ChatMessage", MessageText);
            chatMap.put("User ID", current_uid);
            chatMap.put("Timestamp", FieldValue.serverTimestamp());

            firebaseFirestore.collection("Messages")
                    .document(messageDoc).collection("chats")
                    .add(chatMap)
                    .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                        @Override
                        public void onSuccess(DocumentReference documentReference) {

                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {

                        }
                    });


            // Clear input box
            mMessageEditText.setText("");
        }
    });

}

private void setUpRecyclerView() {
    Query query = colRef.orderBy("Timestamp", Query.Direction.ASCENDING);

    FirestoreRecyclerOptions<Message> options = new FirestoreRecyclerOptions.Builder<Message>()
            .setQuery(query, Message.class)
            .build();

    adapter = new MessageRecyclerAdapter(options);

    RecyclerView recyclerView = findViewById(R.id.recycler_message_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

}

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == GALLERY_PICK && resultCode == RESULT_OK) {

        String imageUri = data.getDataString();

        CropImage.activity(Uri.parse(imageUri))
                .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();

            final String current_user_id = mCurrentUser.getUid();
            final String randomName = UUID.randomUUID().toString();


            final StorageReference filepath = storageReference.child("chat_photos").child(randomName + ".JPG");
            filepath.putFile(resultUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {

                            final String downloadUrl = uri.toString();

                            Map<String, String> newImageUrl = new HashMap<>();
                            newImageUrl.put("image", downloadUrl);


                            firebaseFirestore.collection("Messages").document(TESTmessagedoc).collection("chats")
                                    .add(newImageUrl)
                                    .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                                        @Override
                                        public void onSuccess(DocumentReference documentReference) {

                                            /*
                                            filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                                @Override
                                                public void onSuccess(Uri uri) {

                                                    //The download url
                                                    final String downloadUrl =
                                                            uri.toString();
                                                    Log.d("tag", downloadUrl);
                                                    if (!downloadUrl.equals("default")) {
                                                        // I changed this to glide since i thought picasso was the problem.
                                                        // Picasso still should work. Glide is recommended by google tho
                                                        Glide.with(getApplicationContext()).load(downloadUrl).into(chatProfile);
                                                    }

                                                }
                                            });
                                            */

                                        }
                                    }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(getApplicationContext(), "There was some error in saving Changes.", Toast.LENGTH_LONG).show();
                                }

                            });
                        }
                    });



                }
            });
        }
    }

}}

在模型类中,我添加了getmChatImageURL

我在MessageRecyclerAddapter中添加了setChatImage

我被卡住(我认为)的位置是我onBindViewHolder上的MessageRecyclerAdapter中,我需要对其进行设置,但是我不确定如何去做,因为我不知道如何将其加载到drawable中。 / p>

更新

在我的MessageRecyclerAdapter中,我添加了

    @Override
protected void onBindViewHolder(@NonNull MessageHolder holder, int position, @NonNull Message model) {

    holder.ChatMessage.setText(model.getChatMessage());

    holder.setChatImage(model.getmChatImageURL());

}

该应用程序运行,但是当我将图像上传到Firestore时,该应用程序尝试显示该图像时,我会在日志中获取

W/Glide: Load failed for null with size [0x0] class com.bumptech.glide.load.engine.GlideException: Received null model

图像是作为URL存储在Firestore中的,所以我认为该错误与不确定适配器的设置有关。

更新2

我认为我在正确的轨道上

我更改了我的适配器类

MessageRecyclerAdapter:

public class MessageRecyclerAdapter extends FirestoreRecyclerAdapter<Message, MessageRecyclerAdapter.MessageHolder> {

private Context mcontext;



public MessageRecyclerAdapter(@NonNull FirestoreRecyclerOptions<Message> options ) {

    super(options);


}

@Override
protected void onBindViewHolder(@NonNull MessageHolder holder, int position, @NonNull Message model) {

    holder.ChatMessage.setText(model.getChatMessage());


    Glide.with(mcontext.getApplicationContext())
            .load(model.getmChatImageURL())
            .into(holder.ChatMessageImage);


}

@NonNull
@Override
public MessageHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.message_item, viewGroup, false);

    return new MessageHolder(v);
}

class MessageHolder extends RecyclerView.ViewHolder{

    TextView ChatMessage;
    CircleImageView MessageProfilePicture;
    ImageView ChatMessageImage;



    public MessageHolder(@NonNull View itemView) {
        super(itemView);

        ChatMessage = itemView.findViewById(R.id.Message_Chat_Content);
        MessageProfilePicture = itemView.findViewById(R.id.Chat_Profile_picture);
        ChatMessageImage = itemView.findViewById(R.id.chat_Message_image);

    }


}

}

我将滑动设置图像添加到onbind中,这是我现在在运行代码时面临的错误

android.content.Context android.content.Context.getApplicationContext()' on a null object reference

我也尝试过

Glide.with(holder.ChatMessageImage.getContext())
            .load(model.getmChatImageURL())
            .into(holder.ChatMessageImage);

该应用程序运行后,我能够成功将图像上传到Firestore,但是滑行引发

W/Glide: Load failed for null with size [0x0]
class com.bumptech.glide.load.engine.GlideException: Received null model

我不确定如何调试它,因为当我将调试器挂接到onbind中的滑动代码时,它将在活动启动后立即触发

1 个答案:

答案 0 :(得分:0)

如果您已经知道Estimator,只需像这样将其传递

imageUrl