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์ ์ค๋ค. |
๋๊ธ