问题描述
我正在创建一个聊天应用程序。我的文本消息已成功上传,但是这次我尝试将图像上传到我的聊天应用程序到服务器上。
Guy's I am creating a chat application. My Text messages are uploaded successfully but I am trying to upload an image to my chat application to the server this time I have some error.
即:java.lang。 IllegalStateException:指定的子代已经有一个父代。您必须先在孩子的父母身上调用removeView()。
That is: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
这是我的图像上传代码:
This is my code for image upload:
final String filename = "" + m[0];
String s1 = filename;
String f_name = s1.substring(7);
String type = "image";
DatabaseReference dbs = FirebaseDatabase.getInstance().getReference();
Date today = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
String dateToStr = format.format(today);
ChatMessagepojo chatmsg = new ChatMessagepojo(profileimageurl, dateToStr, type, id, name, f_name);
dbs.child("chats").push().setValue(chatmsg);
alertDialogimage.dismiss();
alertDialog.dismiss();
这是什么问题。
这是我的视图持有人:
package com.blood4pet.app.Adaptor;
import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.blood4pet.app.R;
import com.blood4pet.app.pojo.ChatMessagepojo;
import com.bumptech.glide.Glide;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.util.List;
import static android.os.Environment.DIRECTORY_DOWNLOADS;
import static androidx.constraintlayout.widget.Constraints.TAG;
public class ChatsList extends RecyclerView.Adapter<ChatsList.ViewHolder> {
private static final int MSG_TYPE_LEFT = 0;
private static final int MSG_TYPE_RIGHT = 1;
private static final int MSG_IMAGE_TYPE_RIGHT = 2;
private static final int MSG_IMAGE_TYPE_LEFT = 3;
private Context mContext;
private List<ChatMessagepojo> mChat;
private String name;
String type;
FirebaseUser firebaseUser;
StorageReference storageReference;
StorageReference ref;
public ChatsList(Context mContext, List<ChatMessagepojo> mChat, String name, String type) {
this.mContext = mContext;
this.mChat = mChat;
this.name = name;
this.type = type;
}
@NonNull
@Override
public ChatsList.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == MSG_IMAGE_TYPE_RIGHT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_image_right, parent, false);
return new ChatsList.ViewHolder(view);
}
if (viewType == MSG_IMAGE_TYPE_LEFT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_image_left, parent, false);
return new ChatsList.ViewHolder(view);
}
if (viewType == MSG_TYPE_RIGHT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_right, parent, false);
return new ChatsList.ViewHolder(view);
} else {
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_left, parent, false);
return new ChatsList.ViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
ChatMessagepojo chat = mChat.get(position);
if (mChat.get(position).getSid().equals(firebaseUser.getUid()) && chat.getType().equals("image")) {
Glide.with(mContext).load(chat.getMessage()).into(holder.loadedimage);
holder.my_name.setText(chat.getName());
} else if (mChat.get(position).getSid().equals(firebaseUser.getUid()) && chat.getType().equals("text")) {
holder.show_message.setText(chat.getMessage());
holder.my_name.setText(chat.getName());
} else if (chat.getType().equals("image")) {
Glide.with(mContext).load(chat.getMessage()).into(holder.loadedimage);
holder.my_name.setText(chat.getName());
holder.download_image_file.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedposition = position;
ChatMessagepojo productid = mChat.get(position);
String message = productid.getMessage();
String filename = productid.getImagename();
downloadimage(message, filename);
}
});
} else if (chat.getType().equals("text")) {
holder.show_message.setText(chat.getMessage());
holder.my_name.setText(chat.getName());
}
}
private void downloadimage(String message, String file) {
Log.d(TAG, "image url: " + message);
storageReference = FirebaseStorage.getInstance().getReference().child("chat");
final String filename = file;
Log.d(TAG, "Original File Name: " + filename);
ref = storageReference.child(filename + ".jpg");
Log.d(TAG, "download Url: " + ref.getDownloadUrl());
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();
download(mContext, filename, ".jpg", DIRECTORY_DOWNLOADS, url);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
// download();
}
public void download(Context context, String filename, String file_extension, String destinationDirectory, String url) {
DownloadManager downloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);
request.setDestinationInExternalFilesDir(context, destinationDirectory, filename + file_extension);
Long refeerence = downloadManager.enqueue(request);
}
@Override
public int getItemCount() {
return mChat.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView show_message;
public TextView my_name;
public ImageView loadedimage, download_image_file;
public ViewHolder(@NonNull View itemView) {
super(itemView);
loadedimage = (ImageView) itemView.findViewById(R.id.show_image);
download_image_file = (ImageView) itemView.findViewById(R.id.download_image_file);
show_message = (TextView) itemView.findViewById(R.id.show_message);
my_name = (TextView) itemView.findViewById(R.id.my_name);
}
}
@Override
public int getItemViewType(int position) {
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
ChatMessagepojo chat = mChat.get(position);
String type = chat.getType();
if (mChat.get(position).getSid().equals(firebaseUser.getUid()) && type.equals("text")) {
return MSG_TYPE_RIGHT;
} else if (mChat.get(position).getSid().equals(firebaseUser.getUid()) && type.equals("image")) {
return MSG_IMAGE_TYPE_RIGHT;
} else if (type.equals("image")) {
return MSG_IMAGE_TYPE_LEFT;
} else {
return MSG_TYPE_LEFT;
}
}
}
但是我有一个错误图像浏览按钮单击:
But I have an error on the image browsing button clicking:
btn_attach = (ImageButton) findViewById(R.id.btn_attach);
btn_attach.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog = alert.create();
alertDialog.setTitle("Select File Type");
// alertDialog.getWindow().setGravity(Gravity.BOTTOM);
alertDialog.show();
}
});
推荐答案
我认为您以错误的方式创建了应用程序。您尝试创建聊天应用程序的手段。首先,将图片picer传递到另一个活动中,并在上传图片后再返回同一活动。尝试这种方式。
I think you are creating the application in the wrong way. You try to create a chat application means. first, pass the image picer in another activity and get it after upload image go back it same activity. Try This way.
这篇关于指定的孩子已经有一个父母。您必须先在我的聊天应用程序中对孩子的父母调用removeView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!