英文:
Talking text+images from user and saving it to firebase to display later. (Like quora)
问题
我正在制作一个博客应用程序。我想要使编辑器像 Quora 一样。用户可以输入帖子并在文本之间插入图片。然后,当他点击保存时,我希望将其保存在 Firebase 中,并稍后检索以以用户插入文本和图像的相同顺序显示。
简而言之,就像 Quora 的回答,您可以在其中包含文本和图片。
英文:
I'm making a blogging app. I want to make the editor like quora.
User can type post and insert images in between of texts. Then when he clicks save, I want this to get saved in firebase and later retrieve it to display in the same order the user inserted texts and images.
In short,Like quora answers where you can have text and images.
答案1
得分: 0
编辑:
我现在明白你的问题了。
你想要的是一个包括图像标签在内的HTML 富文本视图。
所以,要显示它,你可以使用 textView.setText(Html.fromHtml(youHtml))
。
要保存它,你可以使用任何第三方的 富文本编辑框 库,它将为你提供一个包含图像和文本的 HTML 结果,用于保存到 Firebase,以表示用户的订单。
一些富文本编辑框库包括:RichTextEditor、richeditor-android、Android-RTEditor。
如果这回答了你的问题,请标记为已解答。
这是关于如何实现上传图像并获取其链接的旧答案
假设你的问题是关于将文本和图像上传到Firebase的Firestore。
首先,你需要将图像上传到Firebase存储并获取下载链接:
// 从我们的应用创建存储引用
StorageReference storageRef = storage.getReference();
Uri file = Uri.fromFile(new File(IMAGE PATH));
final StorageReference riversRef = storageRef.child("images/" + file.getLastPathSegment());
uploadTask = ref.putFile(file);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// 继续执行任务以获取下载链接
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
// TODO 下一步处理
} else {
// 处理失败
// ...
}
}
});
在获取下载链接后,将下载链接和textView上传到Firestore,代码如下:
Map<String, String> post = new HashMap<>();
user.put("text", myPostEditText.getText().toString());
user.put("image", downloadUri);
FirebaseFirestore db = FirebaseFirestore.getInstance();
// 使用生成的ID添加新文档
db.collection("posts")
.add(post)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
Log.d(TAG, "添加的文档ID:" + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "添加文档时出错", e);
}
});
因此,保存和上传这一部分的最终代码应该类似于上面的示例。
英文:
EDIT:
I get your question now.
What you want is a HTML Rich TextView including images tags and all.
So for displaying you'd use textView.setText(Html.fromHtml(youHtml))
and for saving you could use any 3rd party RichEditText library it'll give you an html result to save to firebase including images and text is the user's order
some RichEditTexts :- RichTextEditor, richeditor-android,
Android-RTEditor
Mark answered if this answers your question
This is old answer on how to implement uploading images and get the link for it
assuming your question is about uploading text and image into Firebase's Firestore
First you'll need to upload the image to firebase storage and get the download URI
// Create a storage reference from our app
StorageReference storageRef = storage.getReference();
Uri file = Uri.fromFile(new File(IMAGE PATH));
final StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
uploadTask = ref.putFile(file);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
// TODO NEXT PART
} else {
// Handle failures
// ...
}
}
});
and after getting download uri upload both the uri and the textView to Firestore like this
Map<String, String> post = new HashMap<>();
user.put("text", myPostEditText.getText().toString());
user.put("image", downloadUri);
FirebaseFirestore db = FirebaseFirestore.getInstance();
// Add a new document with a generated ID
db.collection("posts")
.add(post)
so the final code for saving the uploading this should be something like this
// Create a storage reference from our app
StorageReference storageRef = storage.getReference();
Uri file = Uri.fromFile(new File(IMAGE PATH));
final StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
uploadTask = ref.putFile(file);
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
// Continue with the task to get the download URL
return ref.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
Map<String, String> post = new HashMap<>();
user.put("text", myPostEditText.getText().toString());
user.put("image", downloadUri);
FirebaseFirestore db = FirebaseFirestore.getInstance();
// Add a new document with a generated ID
db.collection("posts")
.add(post).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(MainActivity.this, "Uploaded successfully", Toast.LENGTH_SHORT).show();
Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error adding document", e);
}
});
} else {
// Handle failures
// ...
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论