【问题标题】:How to read text from Uri and put in textView? Android Studio - Java如何从 Uri 读取文本并放入 textView?安卓工作室-Java
【发布时间】:2018-11-12 12:02:05
【问题描述】:

我想在 TextView 中显示文本。我有一个 .txt 存储在 Firebase 存储中。我将此 .txt 的 Url 存储在我的 Android Studio 项目(main/res/raw)中的 .xml 中

这是活动中 onCreate 的一些代码。

str = poi.getText();
final Uri uri = Uri.parse(str);
TextView textView = findViewById(R.id.TEXT_STATUS_ID);
textView.setText((CharSequence) uri);

但是我遇到了问题:

Process: com.example.project, PID: 10040
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.POIPresentationActivity}: java.lang.ClassCastException: android.net.Uri$StringUri cannot be cast to java.lang.CharSequence
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
 Caused by: java.lang.ClassCastException: android.net.Uri$StringUri cannot be cast to java.lang.CharSequence
    at com.example.project.POIPresentationActivity.onCreate(POIPresentationActivity.java:117)
    at android.app.Activity.performCreate(Activity.java:6958)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6776) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 

看起来我无法将 Uri 转换为 CharSequence。实现我想做的最好的方法是什么?我的方法适用于图像、音频和视频(因为不需要演员表)

 str = poi.getVideoLink();
 Uri uri = Uri.parse(str);
 VideoView video = findViewById(R.id.video_view);
 video.setMediaController(new MediaController(this));
 video.setVideoURI(uri);
 video.requestFocus();

我看过一些关于 inputStreams 的文档,但不确定这是否是可行的方法。任何见解表示赞赏

使用建议的 toString() 方法无法按预期工作:

 textView.setText((CharSequence) uri);

改为

 textView.setText(uri.toString());

显示文本,但它显示我要显示的文本的链接 (https://firebasestorage.googleapis.com/ ......) - 不是链接指向的 .txt 中包含的文本

【问题讨论】:

  • 改用toString()
  • 请查看更新的问题,谢谢

标签: java firebase android-studio firebase-realtime-database


【解决方案1】:

来自Documentation:

setText (CharSequence text)

所以它接受CharSequence 作为参数但Uri 不是!

因此更改为toString(),它返回此 URI 的编码字符串表示形式。


根据您的更新,您需要 .txt 文件中的文本。

所以首先您需要创建该.txt 文件的引用(这取决于您如何存储文件look here)

并像这样传递上面的uri:

StorageReference httpsReference = storage.getReferenceFromUrl(uri);

注意uri应包含文件的完整地址,例如:

"https://firebasestorage.googleapis.com/b/bucket/..../yourTextFile.txt"

另请注意,如果str 包含URL,则您实际上不需要使用uri,因此请立即将str 传递给引用!

然后像这样下载内存中的.txt文件:

final long ONE_MEGABYTE = 1024 * 1024; // or to the maximum size of your text, but careful it crashes if it's too big
httpsReference.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
    @Override
    public void onSuccess(byte[] bytes) {
        // String s = new String(bytes); should contain the text
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

【讨论】:

  • getText() 返回指向 .txt 的链接的 str,而不是该链接处 .txt 中的文本。这是我的问题 - 我想要 .txt 文件中的文本,而不是指向它的链接
  • 感谢您的赞赏。就像我说的,我认为它涉及输入流。例如,我对视频没有这个问题,因为 VideoView 已经有一个名为 VideoView.setVideoURI(uri) 的方法
猜你喜欢
  • 1970-01-01
  • 2021-09-09
  • 2014-04-30
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 2016-01-26
  • 2018-01-29
  • 2013-07-08
相关资源
最近更新 更多