如何从Uri读取文本并放入textView? Android Studio - Java

时间:2018-06-02 16:08:20

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

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

以下是活动中onCreate的一些代码。

procedure intAdd is new Add_Data (T => Integer, "+" => "+", ...

但是我遇到了问题:

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

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

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) 

我在inputStreams上看到了一些文档,但我不确定这是否可行。任何洞察力赞赏

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

 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();

已更改为

 textView.setText((CharSequence) uri);

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

1 个答案:

答案 0 :(得分:0)

来自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
    }
});