728x90
반응형
외부 저장(External Storage)
- 착탈이 가능한 SD 카드
- 외부 저장 공간에 저장된 파일들은 누구나 읽을 수 있으며, 사용자가 변경 가능
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout01"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="@+id/linearLayout02">
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/editText"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayout03">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/read"
android:text="읽기"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/write"
android:text="쓰기"/>
</LinearLayout>
</LinearLayout>
외부 저장(External Storage)
- 착탈이 가능한 SD 카드
- 외부 저장 공간에 저장된 파일들은 누구나 읽을 수 있으며, 사용자가 변경 가능
package kr.co.fileexternalapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
String FILENAME = "externalfile.txt";
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
//외부 미디어 장착 여부 체크
String state = Environment.getExternalStorageState();
if(state.equals(Environment.MEDIA_MOUNTED) == false){
Toast.makeText(this, "외부 스토리지 마운트 실패", Toast.LENGTH_SHORT).show();
}
editText = findViewById(R.id.editText);
Button readButton = findViewById(R.id.read);
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(getExternalFilesDir(null), FILENAME);
try {
InputStream is = new FileInputStream(file);
byte[] buffer = new byte[is.available()];
is.read(buffer);
editText.setText(new String(buffer));
is.close();
Toast.makeText(getApplicationContext(), "외부 스토리지 읽기 성공", Toast.LENGTH_SHORT).show();
}catch (Exception e ){
e.printStackTrace();
}
}
});
Button writeButton = findViewById(R.id.write);
writeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(getExternalFilesDir(null), FILENAME);
try {
OutputStream os = new FileOutputStream(file);
os.write(editText.getText().toString().getBytes());
os.close();
Toast.makeText(getApplicationContext(), "외부 스토리지 쓰기 성공", Toast.LENGTH_SHORT).show();
} catch (Exception e){
e.printStackTrace();
}
}
});
}
}
[결과 출력]




0123
728x90
반응형
'App > Android Java' 카테고리의 다른 글
Android_Java(17) - MyActivity(2) (0) | 2022.02.10 |
---|---|
Android_Java(16) - MyActivity (0) | 2022.02.10 |
Android_Java(14) - FileApp (0) | 2022.02.09 |
Android_java(13) - AlertDialogApp (0) | 2022.02.09 |
Android_java(12) - OptionMenuApp (0) | 2022.02.09 |
댓글