Talking text+images from user and saving it to firebase to display later. (Like quora)

huangapple go评论87阅读模式
英文:

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.

Like this

答案1

得分: 0

编辑:
我现在明白你的问题了。
你想要的是一个包括图像标签在内的HTML 富文本视图
所以,要显示它,你可以使用 textView.setText(Html.fromHtml(youHtml))

要保存它,你可以使用任何第三方的 富文本编辑框 库,它将为你提供一个包含图像和文本的 HTML 结果,用于保存到 Firebase,以表示用户的订单。
一些富文本编辑框库包括:RichTextEditorricheditor-androidAndroid-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(&quot;images/&quot;+file.getLastPathSegment());
uploadTask = ref.putFile(file);

Task&lt;Uri&gt; urlTask = uploadTask.continueWithTask(new Continuation&lt;UploadTask.TaskSnapshot, Task&lt;Uri&gt;&gt;() {
    @Override
    public Task&lt;Uri&gt; then(@NonNull Task&lt;UploadTask.TaskSnapshot&gt; task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // Continue with the task to get the download URL
        return ref.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener&lt;Uri&gt;() {
    @Override
    public void onComplete(@NonNull Task&lt;Uri&gt; 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&lt;String, String&gt; post = new HashMap&lt;&gt;();
user.put(&quot;text&quot;, myPostEditText.getText().toString());
user.put(&quot;image&quot;, downloadUri);

FirebaseFirestore db = FirebaseFirestore.getInstance();
// Add a new document with a generated ID
db.collection(&quot;posts&quot;)
        .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(&quot;images/&quot;+file.getLastPathSegment());
        uploadTask = ref.putFile(file);

        Task&lt;Uri&gt; urlTask = uploadTask.continueWithTask(new Continuation&lt;UploadTask.TaskSnapshot, Task&lt;Uri&gt;&gt;() {
            @Override
            public Task&lt;Uri&gt; then(@NonNull Task&lt;UploadTask.TaskSnapshot&gt; task) throws Exception {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }

                // Continue with the task to get the download URL
                return ref.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener&lt;Uri&gt;() {
            @Override
            public void onComplete(@NonNull Task&lt;Uri&gt; task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    Map&lt;String, String&gt; post = new HashMap&lt;&gt;();
                    user.put(&quot;text&quot;, myPostEditText.getText().toString());
                    user.put(&quot;image&quot;, downloadUri);

                    FirebaseFirestore db = FirebaseFirestore.getInstance();
// Add a new document with a generated ID
                    db.collection(&quot;posts&quot;)
                            .add(post).addOnSuccessListener(new OnSuccessListener&lt;DocumentReference&gt;() {
                        @Override
                        public void onSuccess(DocumentReference documentReference) {
                            Toast.makeText(MainActivity.this, &quot;Uploaded successfully&quot;, Toast.LENGTH_SHORT).show();
                            Log.d(TAG, &quot;DocumentSnapshot added with ID: &quot; + documentReference.getId());
                        }
                    })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Log.w(TAG, &quot;Error adding document&quot;, e);
                                }
                            });
                } else {
                    // Handle failures
                    // ...
                }
            }
        });

huangapple
  • 本文由 发表于 2020年7月25日 22:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63089755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定