수색…


통사론

  • FileOutputStream openFileInput (캐릭터 라인 명)
  • FileOutputStream openFileOutput (캐릭터 라인 명, int 모드)
  • 파일 (파일 디렉토리, 문자열 이름)
  • 파일 (문자열 경로)
  • 파일 getExternalStoragePublicDirectory (String 타입)
  • File getExternalFilesDir (String 타입)

매개 변수

매개 변수 세부
이름 열 파일의 이름입니다. 참고 : 경로 구분 기호를 포함 할 수 없습니다.
방법 작동 모드. 사용 MODE_PRIVATE 기본 동작 및 MODE_APPEND 기존 파일을 추가 할 수 있습니다. 다른 모드로는 MODE_WORLD_READABLEMODE_WORLD_WRITEABLE 이 있으며 API 17에서는 둘 다 사용되지 않습니다.
지시 새 파일을 만들 파일의 디렉토리
통로 새 파일의 위치를 ​​지정하는 경로
유형 검색 할 파일 디렉토리의 유형. null 또는 다음 중 하나 일 수 있습니다. DIRECTORY_MUSIC , DIRECTORY_PODCASTS , DIRECTORY_RINGTONES , DIRECTORY_ALARMS , DIRECTORY_NOTIFICATIONS , DIRECTORY_PICTURES 또는 DIRECTORY_MOVIES

내부 저장소 사용

기본적으로 내부 저장소에 저장하는 모든 파일은 응용 프로그램에 대한 개인 파일입니다. 다른 응용 프로그램이나 일반 상황에서는 사용자가 액세스 할 수 없습니다. 이러한 파일은 사용자가 응용 프로그램을 제거 할 때 삭제됩니다 .

파일에 텍스트 쓰기

String fileName= "helloworld";
String textToWrite = "Hello, World!";
FileOutputStream fileOutputStream;

try {
  fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
  fileOutputStream.write(textToWrite.getBytes());
  fileOutputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

기존 파일에 텍스트를 추가하는 방법

openFileOutput 의 mode 매개 변수에 Context.MODE_APPEND 를 사용하십시오.

fileOutputStream = openFileOutput(fileName, Context.MODE_APPEND);

외부 저장소 사용

"외부"저장소는 파일을 사용자의 장치에 저장하는 데 사용할 수있는 또 다른 유형의 저장소입니다. "Internal"Storage와는 몇 가지 중요한 차이점이 있습니다 :

  • 항상 사용할 수있는 것은 아닙니다. 이동식 매체 (SD 카드)의 경우 사용자는 단순히 저장소를 제거 할 수 있습니다.
  • 그것은 사적인 것이 아닙니다. 사용자 (및 기타 응용 프로그램)는이 파일에 액세스 할 수 있습니다.
  • 사용자가 앱을 제거하면 getExternalFilesDir() 을 사용하여 검색 한 디렉토리에 저장 한 파일이 제거됩니다.

외부 저장소를 사용하려면 먼저 적절한 권한을 얻어야합니다. 다음을 사용해야합니다.

이러한 권한을 부여하려면 AndroidManifest.xml 에서 해당 권한을 식별해야합니다

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

참고 : API 레벨 23 이상을 사용하는 경우 위험한 권한 이므로 런타임에 사용 권한 을 요청해야 합니다 .

외부 저장소에 쓰거나 읽기를 시도하기 전에 항상 저장 매체가 사용 가능한지 확인해야합니다.

String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
    // Available to read and write
}
if (state.equals(Environment.MEDIA_MOUNTED) || 
        state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
    // Available to at least read
}

외부 저장소에 파일을 쓸 때 파일을 공개 또는 비공개로 인식해야할지 결정해야합니다. 이러한 유형의 파일은 모두 사용자 및 장치의 다른 응용 프로그램에서 계속 액세스 할 수 있지만 두 파일 간에는 중요한 차이가 있습니다.

사용자가 앱을 제거하면 공개 파일이 기기에 남아 있어야합니다. 공개로 저장해야하는 파일의 예는 응용 프로그램을 통해 가져온 사진입니다.

사용자가 앱을 제거하면 개인 파일이 모두 삭제됩니다. 이러한 유형의 파일은 앱에 따라 달라지며 사용자 또는 다른 애플리케이션에 유용하지 않을 수 있습니다. 전의. 응용 프로그램에서 다운로드 / 사용하는 임시 파일.

공개 및 비공개 파일 모두에 대해 Documents 디렉토리에 액세스하는 방법은 다음과 같습니다.

공공의

// Access your app's directory in the device's Public documents directory
File docs = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_DOCUMENTS), "YourAppDirectory");
// Make the directory if it does not yet exist
myDocs.mkdirs();

은밀한

// Access your app's Private documents directory
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), 
        "YourAppDirectory");
// Make the directory if it does not yet exist
myDocs.mkdirs();

Android : 내부 및 외부 저장소 - 용어 설명

안드로이드 개발자 (주로 초보자)는 내부 및 외부 저장 용어에 대해 혼란스러워합니다. 같은 Stackoverflow에 관한 많은 질문이 있습니다. 이는 주로 Google / 공식 Android 설명서에 따른 용어 가 일반 Android OS 사용자의 용어 와 상당히 다르다는 사실 때문에 주로 발생합니다. 그러므로 나는 이것을 문서화하는 것이 도움이 될 것이라고 생각했다.

우리가 생각하는 것 - 사용자 용어 (UT)

내부 저장소 (UT) 외부 저장소 (UT)
휴대 전화 내장 내장 메모리 착탈식 SD (Secure Digital) 카드 또는 마이크로 SD 저장 장치
예 : Nexus 6P의 32GB 내부 메모리 예 : 삼성, 샌디 스크, 스트론튬, 초월 등의 공급 업체가 제공하는 이동식 SD 카드의 저장 공간

하지만 Android 설명서 / 가이드 - Google 용어 (GT)에 따르면 ,

내부 저장 장치 (GT) :

기본적으로 내부 저장소에 저장된 파일은 애플리케이션 전용이며 다른 애플리케이션은 액세스 할 수 없으며 사용자도 액세스 할 수 없습니다.

외부 저장소 (GT) :

이동식 저장 매체 (예 : SD 카드) 또는 내부 (비 분리형) 저장 장치가 될 수 있습니다.

외부 저장소 (GT)는 두 가지 유형으로 분류 할 수 있습니다.

주요 외부 저장소 보조 외부 저장소 또는 이동식 저장소 (GT)
이것은 전화기의 내장 내장 메모리 (또는) 내부 저장 장치 (UT)와 동일합니다. 이동식 마이크로 SD 카드 저장 장치 (또는 외부 저장 장치 (UT))와 동일합니다.
예 : Nexus 6P의 32GB 내부 메모리 예 : 삼성, 샌디 스크, 스트론튬, 초월 등의 공급 업체가 제공하는 이동식 SD 카드의 저장 공간
이 유형의 저장소는 USB 케이블을 통해 PC에 전화를 연결하고 USB 옵션 알림에서 카메라 (PTP) 를 선택하면 Windows PC에서 액세스 할 수 있습니다. 이 유형의 저장소는 USB 케이블을 통해 PC에 전화를 연결하고 USB 옵션 알림에서 파일 전송 을 선택하면 Windows PC에서 액세스 할 수 있습니다.

요컨대,

외부 저장소 (GT) = 내부 저장소 (UT) 및 외부 저장소 (UT)

이동식 저장소 (GT) = 외부 저장소 (UT)

내부 저장 장치 (GT)에는 UT라는 용어가 없습니다.

제가 분명히 설명해 드리겠습니다.

내부 저장소 (GT) : 기본적으로 내부 저장소에 저장된 파일은 애플리케이션 전용이며 다른 애플리케이션에서는 액세스 할 수 없습니다. 또한 앱 사용자는 파일 관리자를 사용하여 액세스 할 수 없습니다. 파일 관리자에서 "숨김 파일 표시"옵션을 활성화 한 후에도 내부 저장소 (GT)의 파일에 액세스하려면 Android 휴대 전화를 루트해야합니다. 또한 사용자가 응용 프로그램을 제거하면 이러한 파일이 제거 / 삭제됩니다.

따라서 내부 저장 장치 (GT)는 Nexus 6P의 32/64 GB 내부 메모리로 생각 하지 않습니다.

일반적으로 내부 저장소 (GT) 위치 는 다음과 같습니다. /data/data/your.application.package.appname/someDirectory/

외부 저장 장치 (GT) :

모든 Android 호환 장치는 파일을 저장하는 데 사용할 수있는 공유 된 "외부 저장소"를 지원합니다. 외부 저장 장치에 저장된 파일은 누구나 읽을 수 있으며 USB 대용량 저장 장치가 컴퓨터의 파일을 전송할 수 있도록 설정할 때 사용자가 수정할 수 있습니다.

외부 저장소 (GT) 위치 : 내부 저장소 (UT) 또는 이동식 저장소 (예 : 마이크로 SD 카드)의 어느 위치 에나 있을 수 있습니다. 휴대 전화의 OEM 및 Android OS 버전에 따라 다릅니다.

외부 저장소 (GT)에서 파일을 읽거나 쓰려면 앱에서 READ_EXTERNAL_STORAGE 또는 WRITE_EXTERNAL_STORAGE 시스템 권한을 얻어야합니다.

예 :

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

파일을 읽고 쓰는 것이 모두 필요한 경우 암시 적으로 읽기 액세스가 필요하기 때문에 WRITE_EXTERNAL_STORAGE 권한 만 요청하면됩니다.

외부 저장 장치 (GT) 에서 앱 비공개 파일을 저장할 수도 있습니다

그러나,

사용자가 응용 프로그램을 제거하면이 디렉토리와 모든 내용이 삭제됩니다.

외부 저장 장치 (GT)응용 프로그램 전용 파일을 언제 저장해야합니까?

다른 앱에서 사용할 수없는 파일 (그래픽 텍스처 또는 앱에서만 사용하는 사운드 효과 등)을 처리하는 경우 외부 저장소의 개인 저장소 디렉토리를 사용해야합니다

Android 4.4부터 앱의 개인 디렉토리에서 파일을 읽거나 쓰는 데 READ_EXTERNAL_STORAGE 또는 WRITE_EXTERNAL_STORAGE 권한이 필요하지 않습니다. 따라서 maxSdkVersion 속성을 추가하여 Android의 하위 버전에서만 권한을 요청해야한다고 선언 할 수 있습니다.

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                     android:maxSdkVersion="18" />
    ...
</manifest

내부 저장소 (GT)에 저장하는 방법 :

이 두 메소드는 모두 Context 클래스에 있습니다.

File getDir (String name, int mode)

File getFilesDir () 

기본 외부 저장소 (즉, 내부 저장소 (UT))에 저장하는 방법 :

File getExternalStorageDirectory ()

File getExternalFilesDir (String type)

File getExternalStoragePublicDirectory (String type)

처음에는 모든 사람들이 Primary External Storage루트 를 가리키는 Environment.getExternalStorageDirectory ()를 사용했습니다. 그 결과, 주요 외부 저장소는 임의의 콘텐츠로 채워졌습니다.

나중에이 두 가지 방법이 추가되었습니다.

  1. Context 클래스에서 getExternalFilesDir () 을 추가하여 Primary External Storage의 응용 프로그램 별 디렉토리 를 가리 킵니다. 이 디렉토리와 내용 앱을 제거하면 삭제 됩니다.

  2. Environment.getExternalStoragePublicDirectory () 사진 및 영화와 같이 잘 알려진 파일 유형을 저장하기위한 중앙 집중식 장소입니다. 이 디렉토리와 내용 앱을 제거 할 때 삭제되지 않습니다 .

이동식 저장소 (GT), 즉 마이크로 SD 카드에 저장하는 방법

API 레벨 19 이전 에는 공식적인 SD 카드 저장 방법없었습니다 . 그러나 많은 사람들이 비공식 라이브러리 또는 API를 사용하여이를 수행 할 수 있습니다.

공식적으로 한 가지 방법이 API 수준 19 (Android 버전 4.4 - Kitkat)의 Context 클래스에 도입되었습니다.

File[] getExternalFilesDirs (String type)

응용 프로그램이 소유 한 지속적 파일을 저장할 수있는 모든 공유 / 외부 저장 영역 디바이스의 응용 프로그램 특정 디렉토리에 대한 절대 경로를 리턴합니다. 이러한 파일은 응용 프로그램 내부에 있으며 일반적으로 미디어에 사용자가 볼 수 없습니다.

즉, 내부 메모리와 마이크로 SD 카드의 가지 외장형 스토리지 (GT) 경로를 반환합니다. 일반적으로 두 번째 경로 는 마이크로 SD 카드의 저장 경로입니다 (항상 그런 것은 아닙니다). 따라서이 메소드로 코드를 실행하여 체크 아웃해야합니다.

코드 스 니펫의 예 :

빈 액티비티로 새로운 안드로이드 프로젝트를 만들었습니다. 다음 코드를 썼습니다.

MainActivity.java protected void onCreate(Bundle savedInstanceState) 메소드

    File internal_m1 = getDir("custom", 0);
    File internal_m2 = getFilesDir();

    File external_m1 =  Environment.getExternalStorageDirectory();
    
    File external_m2 =  getExternalFilesDir(null);
    File external_m2_Args = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

    File external_m3 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    
    File[] external_AND_removable_storage_m1 = getExternalFilesDirs(null);
    File[] external_AND_removable_storage_m1_Args = getExternalFilesDirs(Environment.DIRECTORY_PICTURES);

위의 코드를 실행 한 후,

산출:

internal_m1: /data/data/your.application.package.appname/app_custom

internal_m2: /data/data/your.application.package.appname/files

external_m1: /storage/emulated/0

external_m2: /storage/emulated/0/Android/data/your.application.package.appname/files

external_m2_Args: /storage/emulated/0/Android/data/your.application.package.appname/files/Pictures

external_m3: /storage/emulated/0/Pictures

external_AND_removable_storage_m1 (first path):
/storage/emulated/0/Android/data/your.application.package.appname/files

external_AND_removable_storage_m1 (second path):    
/storage/sdcard1/Android/data/your.application.package.appname/files

 external_AND_removable_storage_m1_Args (first path):
/storage/emulated/0/Android/data/your.application.package.appname/files/Pictures

external_AND_removable_storage_m1_Args (second path): /storage/sdcard1/Android/data/your.application.package.appname/files/Pictures

참고 : 저는 휴대폰을 Windows PC에 연결했습니다. 개발자 옵션과 USB 디버깅을 모두 사용할 수있게 한 다음이 코드를 실행했습니다. 전화기를 연결하지 않은 경우; 대신 Android 에뮬레이터 에서 실행하면 결과가 다를 수 있습니다. 내 휴대 전화 모델은 Coolpad Note 3 - Android 5.1에서 실행 중입니다.

휴대 전화의 저장 위치 :

마이크로 SD 저장 위치 : /storage/sdcard1

내부 저장소 (UT) 위치 : /storage/sdcard0 .

/sdcard/storage/emulated/0 도 내부 저장소 (UT)를 가리 킵니다. 그러나 이것들은 /storage/sdcard0 대한 심볼릭 링크입니다.

Android에서 다양한 저장 경로를 명확하게 이해하려면 이 답변을 확인 하시기 바랍니다.

면책 조항 : 위에서 언급 한 모든 스토리지 경로는 휴대 전화의 경로입니다. 파일이 동일한 저장 경로에 저장 되지 않을 수 있습니다. 저장 위치 / 경로는 공급 업체, 제조업체 및 Android OS의 여러 버전에 따라 다른 휴대폰에서 다를 수 있습니다.

SD 카드에 데이터베이스 저장 (SD에 백업 DB)

    public static Boolean ExportDB(String DATABASE_NAME , String packageName , String folderName){
    //DATABASE_NAME including ".db" at the end like "mayApp.db"
    String DBName = DATABASE_NAME.substring(0, DATABASE_NAME.length() - 3);
    File data = Environment.getDataDirectory();
    FileChannel source=null;
    FileChannel destination=null;
    String currentDBPath = "/data/"+ packageName +"/databases/"+DATABASE_NAME; // getting app db path

    File sd = Environment.getExternalStorageDirectory(); // getting phone SD card path
    String backupPath = sd.getAbsolutePath() + folderName; // if you want to set backup in specific folder name
        /* be careful , foldername must initial like this : "/myFolder" . dont forget "/" at begin of folder name
            you could define foldername like this : "/myOutterFolder/MyInnerFolder" and so on ...
        */
    File dir = new File(backupPath);
    if(!dir.exists()) // if there was no folder at this path , it create it .
    {
        dir.mkdirs();
    }

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    Date date = new Date();
        /* use date including file name for arrange them and preventing to make file with the same*/
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(backupPath, DBName +"("+ dateFormat.format(date)+").db");
    try {
        if (currentDB.exists() && !backupDB.exists()) {
            source = new FileInputStream(currentDB).getChannel();
            destination = new FileOutputStream(backupDB).getChannel();
            destination.transferFrom(source, 0, source.size());
            source.close();
            destination.close();
            return true;
        }
        return false;
    } catch(IOException e) {
        e.printStackTrace();
        return false;
    }
}

이 방법은 다음과 같이 호출하십시오.

ExportDB ( "myDB.db", "com.example.exam", "/ myFolder");

장치 디렉토리 가져 오기 :

먼저 장치 디렉터리를 읽거나 가져올 저장소 권한을 추가합니다.

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

모델 클래스 만들기

//create one directory model class
//to store directory title and type in list

public class DirectoryModel {
    String dirName;
    int dirType; // set 1 or 0, where 0 for directory and 1 for file.

    public int getDirType() {
        return dirType;
    }

    public void setDirType(int dirType) {
        this.dirType = dirType;
    }

    public String getDirName() {
        return dirName;
    }

    public void setDirName(String dirName) {
        this.dirName = dirName;
    }
}

디렉토리 모델을 사용하여 목록을 작성하여 디렉토리 데이터를 추가하십시오.

//define list to show directory

List<DirectoryModel> rootDir = new ArrayList<>();

다음 메소드를 사용하여 디렉토리를 가져 오십시오.

//to fetch device directory

private void getDirectory(String currDir) { // pass device root directory
        File f = new File(currDir);
        File[] files = f.listFiles();
        if (files != null) {
            if (files.length > 0) {
                rootDir.clear();
                for (File inFile : files) {
                    if (inFile.isDirectory()) { //return true if it's directory
                        // is directory
                        DirectoryModel dir = new DirectoryModel();
                        dir.setDirName(inFile.toString().replace("/storage/emulated/0", ""));
                        dir.setDirType(0); // set 0 for directory
                        rootDir.add(dir);
                    } else if (inFile.isFile()) { // return true if it's file
                        //is file
                        DirectoryModel dir = new DirectoryModel();
                        dir.setDirName(inFile.toString().replace("/storage/emulated/0", ""));
                        dir.setDirType(1); // set 1 for file
                        rootDir.add(dir);
                    }
                }
            }
            printDirectoryList();
        }
    }

로그에 디렉토리 목록을 인쇄하십시오.

//print directory list in logs

private void printDirectoryList() {
    for (int i = 0; i < rootDir.size(); i++) {
        Log.e(TAG, "printDirectoryLogs: " + rootDir.get(i).toString());
    }
}

용법

//to Fetch Directory Call function with root directory.

String rootPath = Environment.getExternalStorageDirectory().toString(); // return ==>  /storage/emulated/0/
getDirectory(rootPath );

특정 디렉토리의 내부 파일 / 폴더를 가져 오려면 동일한 방법을 사용하여 인수를 변경하고 현재 선택된 경로를 인수로 전달하고 동일한 응답을 처리합니다.

파일 확장명을 가져 오려면 다음을 수행하십시오.

private String getExtension(String filename) {

    String filenameArray[] = filename.split("\\.");
    String extension = filenameArray[filenameArray.length - 1];
    Log.d(TAG, "getExtension: " + extension);

    return extension;
}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow