사용한 툴 : 이클립스

사용 버전 : API 16

첨부 파일 : AndSDcardTest.zip


먼저 컴퓨터 내에 AVD로 할 경우 AVD의 SDCard의 용량을 지정해주어야 한다. (실제 용량을 차지하므로 주의)


▲ 설정후 확인버튼을 해주면 된다.


SD카드를 사용하려면 AndroidManifest.xml에서 퍼미션을 지정해주어야 한다.

AndroidManifest.xml

1
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
cs

AndroidManifest에 application 위에 기재하면 된다.


activity_main.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.andsdcardtest.MainActivity"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btnRead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="읽기"
        />
    <EditText 
        android:id = "@+id/edtSD"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:lines="10"/>
</LinearLayout>
cs




MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
package com.andsdcardtest;
 
import java.io.FileInputStream;
import java.io.IOException;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity {
    Button btnRead;
    EditText edtSd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setContentView(R.layout.activity_main);
        btnRead=(Button)findViewById(R.id.btnRead);
        edtSd = (EditText)findViewById(R.id.edtSD);
        
        btnRead.setOnClickListener(new OnClickListener() {        
            @Override
            public void onClick(View v) {
                try {
                    FileInputStream infs= new FileInputStream("/sdcard/file.txt");
                    byte[] temp =new byte[infs.available()];
                    infs.read(temp);
                    infs.close();
                    edtSd.setText(new String(temp));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
        });
    }
 
}
 
cs


AndSDcardTest.zip


+ Recent posts