如何从Android中的.txt文件获取数据

时间:2019-07-10 09:15:08

标签: java android firebase firebase-storage

我正在制作一个应用程序,其中已在Firebase Storage中上载了文本文件,现在我想检索这些文件并在回收者视图内的文本视图中显示这些txt文件的内容。我可以获取这些文本文件的uri,但是如何显示这些文件的内容。

这是我到目前为止完成的代码

public class RecyclerViewAdapter3 extends RecyclerView.Adapter<RecyclerViewAdapter3.ViewHolder> {

Context context3;
List<ImageUploadInfo> MainImageUploadInfoList;


public RecyclerViewAdapter3(Context context, List<ImageUploadInfo> TempList) {

    this.MainImageUploadInfoList = TempList;

    this.context3 = context;
}

public RecyclerViewAdapter3.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items3, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);

    return viewHolder;
}

@Override
public int getItemCount() {

    return MainImageUploadInfoList.size();
}

@Override
public void onBindViewHolder(final RecyclerViewAdapter3.ViewHolder holder, int position) {
    ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);

    Uri uri = Uri.parse(UploadInfo.getImageURL());
    Log.d("TAGGG" , ""+uri);

    holder.userText.setText(uri.toString());
    holder.imageNameTextView.setText(UploadInfo.getImageName());
}

class ViewHolder extends RecyclerView.ViewHolder {
    public TextView imageNameTextView ;
    public TextView userText;


    public ViewHolder(View itemView) {
        super(itemView);

        userText = (TextView) itemView.findViewById(R.id.textView13);

        imageNameTextView = (TextView) itemView.findViewById(R.id.caption);
    }
}
}

在这段代码中,我在文本视图中设置了uri。如何将文本文件的内容设置为textview?

请帮助

2 个答案:

答案 0 :(得分:0)

将文件更改为JSON格式。

{
     "data" : "your text here"
}

并使用通用方法下载数据并解析json(我建议使用Retrofit)。

答案 1 :(得分:0)

运行此线程,您将从文本文件中获取数据

new Thread() {
            @Override
            public void run() {
                String path =/*Pass the url of Firebase*/;
                URL u = null;
                try {
                    u = new URL(path);
                    HttpURLConnection c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("GET");
                    c.connect();
                    InputStream in = c.getInputStream();
                    final ByteArrayOutputStream bo = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    in.read(buffer); // Read from Buffer.
                    bo.write(buffer); // Write Into Buffer.

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TextView text = (TextView) findViewById(R.id.TextView1);
                            text.setText(bo.toString()); /*I have issue with this that it gives some unknown symbol at end*/
                            try {
                                bo.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();