Demo
MainActivity.java
package com.example.recyclerview;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private Button btnSave, btnLoad;
private EditText etInput;
private TextView tvLoad;
private String filename = "";
private String filepath = "";
private String fileContent = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSave = findViewById(R.id.btn_save);
btnLoad = findViewById(R.id.btn_load);
etInput = findViewById(R.id.et_input);
tvLoad = findViewById(R.id.tv_load);
filename = "myFile.txt";
filepath = "MyFileDir";
if(!isExternalStorageAvailableForRW()) {
btnSave.setEnabled(false);
}
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvLoad.setText("");
fileContent = etInput.getText().toString().trim();
if(!fileContent.equals("")) {
File myExternalFile = new File(getExternalFilesDir(filepath), filename);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myExternalFile);
fos.write(fileContent.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
etInput.setText("");
Toast.makeText(MainActivity.this,"Information saved to SD card", Toast.LENGTH_SHORT);
} else {
Toast.makeText(MainActivity.this,"Text field can not be empty", Toast.LENGTH_SHORT);
}
}
});
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FileReader fr = null;
File myExternalFile = new File(getExternalFilesDir(filepath), filename);
StringBuilder stringBuilder = new StringBuilder();
try {
fr = new FileReader(myExternalFile);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
stringBuilder.append(line).append('\n');
line = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
String fileContents = "File contents\n" + stringBuilder.toString();
tvLoad.setText(fileContents);
}
}
});
}
private boolean isExternalStorageAvailableForRW() {
String extStorageState = Environment.getExternalStorageState();
if(extStorageState.equals(Environment.MEDIA_MOUNTED)){
return true;
}
return false;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type here to save on SD card"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="Save"/>
<Button
android:id="@+id/btn_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="Load"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="80dp">
<TextView
android:id="@+id/tv_load"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
</ScrollView>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.recyclerview">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:networkSecurityConfig="@xml/network_security_config"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.RecyclerView">
<!--android:screenOrientation="landscape"
android:turnScreenOn="true"-->
<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
'System Programming > Android' 카테고리의 다른 글
[Android/Java] 날씨 api (0) | 2021.06.14 |
---|---|
[Android/Java] 타이머 (0) | 2021.06.14 |
[Android/Java] Text To Speech (0) | 2021.06.14 |
[Android/Java] 음성인식 해서 글로 변환 (0) | 2021.06.14 |
[Android/Java] Sdcard에 이미지 다운로드 (0) | 2021.06.14 |