๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’š ๊ฐœ๋ฐœ/์•ˆ๋“œ๋กœ์ด๋“œ

[์•ˆ๋“œ๋กœ์ด๋“œ] sdcard์— ๋””๋ ‰ํ† ๋ฆฌ์™€ ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ

by devshin.kr 2021. 7. 12.
728x90

โ€ป Android11(Andorid API Level 30) ๋ถ€ํ„ฐ๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ๋ณด์•ˆ ์ •์ฑ… ์ƒ sdcard์— ์ ‘๊ทผ์ด ์–ด๋ ค์›Œ์กŒ๋‹ค. ๋”ฐ๋ผ์„œ ํ…Œ์ŠคํŠธ ์• ๋ฎฌ๋ ˆ์ดํ„ฐ๋Š” API Level 29 ์ดํ•˜๋กœ ํ•œ๋‹ค.

 

 

sdcard ์•„๋ž˜์— testDir ์ด๋ฆ„์˜ ํด๋”๊ฐ€ ์—†๋‹ค๋ฉด ํ•ด๋‹น ์ด๋ฆ„์˜ ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๊ณ ,

testFile.txt ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ํ›„,

๋‚ด์šฉ์— Hello World! ๋ฅผ ์“ฐ๋Š” ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.

 

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:onClick="writeFile"
        android:text="write File"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</LinearLayout>

 

 

MainActivity.java

package com.devshin93.adbpushpull;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void writeFile (View view) {

        String sPath = "/sdcard/testDir/";
        String sTitle = "testFile.txt";
        String sContent = "Hello World!";

        try {
            File fPath = new File( sPath );

            if (!fPath.exists())
                if(!fPath.mkdirs())
                    throw new Exception("File Path is not invalid.");

            FileOutputStream fo = new FileOutputStream( sPath + sTitle );
            fo.write(sContent.getBytes(), 0, sContent.getBytes().length);
            fo.flush();
            fo.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e2) {
            e2.printStackTrace();
        }
    }
}

 

 

AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.devshin93.adbpushpull">

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

    <application
        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.ADBPushPull">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

AndroidMenifest.xml ์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ ์—ฌ๊ธฐ!

 

 

 

 

๊ฒฐ๊ณผ

 

 

sdcard (์™ธ๋ถ€์ €์žฅ์†Œ)์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์• ๋ฎฌ๋ ˆ์ดํ„ฐ ์„ธํŒ… ๋ฐฉ๋ฒ•

1. ํ•ด๋‹น ์•ฑ์„ ๋งˆ์šฐ์Šค ์ขŒ์ธก์„ ๊พน ๋ˆŒ๋Ÿฌ App info ๋ฅผ ๋ˆ„๋ฅธ๋‹ค. 2. ๊ฐ€์žฅ ์ฒ˜์Œ์—๋Š” Pemissions ์— No permissions granted ๋กœ ๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ๋ฅผ ๋ˆ„๋ฅธ๋‹ค. 3. Storage ์— permission์„ ์ค€๋‹ค.

๋Œ“๊ธ€