Little Bear's note

about radio, computer, and so on...主に無線と電子工作についてのメモ。

CSVファイル表示アプリ for Android

はじめに

気分転換の副産物でCSVファイル表示アプリができました。

参考:Android デベロッパー、(この周辺https://developer.android.com/training/data-storage/shared/documents-files

MainActivity.java

package com.example.csv_reader;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
    private Uri fileUri;
    private Button button_openFile, button_reset;
    private String fileContents;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button_openFile = findViewById(R.id.button);
        button_openFile.setOnClickListener((View.OnClickListener) new selectFile());
        button_reset = findViewById(R.id.button2);
        button_reset.setOnClickListener((View.OnClickListener) new resetScreen());
    }

    public class selectFile implements View.OnClickListener {
        public void onClick(View v){
            file fileOpen = new file();
            fileOpen.openFile();

        }
    }

    public class resetScreen implements View.OnClickListener{
        public void onClick(View v){
            fileContents = null;
            TextView maincontents = findViewById(R.id.textView2);
            maincontents.setText("Contents will appear here...");
        }
    }

    public class file {
        /***外部ストレージのCSVファイルを読む***/
        private void openFile()  {
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setType("text/*");
            startActivityForResult(intent, 1021);

        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        super.onActivityResult(requestCode, resultCode, resultData);
       if (requestCode == 1021 && resultCode == Activity.RESULT_OK) {
            // The result data contains a URI for the document or directory that
            // the user selected.
            fileUri = null;
            if (resultData != null) {
                fileUri = resultData.getData(); //ここでファイルのuriを変数に格納するば、activity内で使える。
                StringBuilder stringBuilder = new StringBuilder();
                try (InputStream inputStream =
                             getContentResolver().openInputStream(fileUri);
                     BufferedReader reader = new BufferedReader(
                             new InputStreamReader(Objects.requireNonNull(inputStream)))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        stringBuilder.append(line).append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                fileContents = stringBuilder.toString();
                TextView textCSV = findViewById(R.id.textView2);
                textCSV.setText(fileContents);
            }
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/layout_top"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="open" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="RESET" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_main"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/layout_top">

        <ScrollView
            android:id="@+id/main_scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/layout_v"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <HorizontalScrollView
                    android:id="@+id/main_scroll_h"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <LinearLayout
                        android:id="@+id/layout_h"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Contents will appear here..." />
                    </LinearLayout>
                </HorizontalScrollView>

            </LinearLayout>
        </ScrollView>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>