我正在制作一个博客应用程序。我想让编辑像quora一样。用户可以在文本之间键入帖子和插入图像。然后,当他点击保存时,我希望这个保存在firebase中,然后检索它,以用户插入文本和图像的相同顺序显示。简言之,像quora一样回答,你可以有文本和图像。
像这个
编辑:我现在明白你的问题了。您想要的是一个HTMLRich TextView,包括图像标记和所有内容。所以为了显示,你应该使用textView.setText(Html.fromHtml(youHtml))
为了保存,您可以使用任何第三方RichEditText库,它会给您一个保存到firebase的html结果,包括图像和文本,这是用户的命令一些RichEditTexts:-RichTextEditor,richeditor android,安卓RTEditor
如果这回答了你的问题,马克回答了
这是关于如何实现上传图像并获取链接的老答案假设您的问题是关于将文本和图像上传到Firebase的Firestore首先,您需要将图像上传到firebase存储,并获得下载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
// ...
}
}
});
在获得下载uri后,将uri和textView都上传到Firestore,就像一样
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)
所以保存上传的最后一个代码应该是这样的
// 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
// ...
}
}
});