作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
伙计,我正在创建一个聊天应用程序。我的短信已成功上传,但这次我尝试将图像上传到我的聊天应用程序到服务器时出现一些错误。
即:java.lang.IllegalStateException: 指定的 child 已经有一个 parent 。您必须先对 child 的 parent 调用 removeView()。
这是我的图片上传代码:
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;
}
}
}
但是我在点击图片浏览按钮时出错:
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();
}
});
最佳答案
我认为您以错误的方式创建了应用程序。您尝试创建一个聊天应用程序的方式。首先,在另一个 Activity 中传递 image picer,并在上传图片返回同一个 Activity 后获取它。尝试这种方式。
关于android - 指定的子项已有父项。您必须先在我的聊天应用程序中对 child 的 parent 调用 removeView(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58117428/
我创建的 MySQL 备份文件是使用 --all-databases 转储的,并且其中包含“CREATE DATABASE IF NOT EXISTS”语句。 我现在有一个全新的 MySQL 实例,我
paragraph
添加到 div 中,但如果该 div 已有 2 个段落,则创建一个新 div我有一个 div .content,里面有两个 div (.content-1, .content-2)。 在 .content 内的 div 中,我只想最多有两个段落。 因此,当我单击“添加”按钮时
我是一名优秀的程序员,十分优秀!