2012년 7월 4일 수요일

[안드로이드] SD 존재 확인

출처 : http://www.androes.com/141


일반적으로 외장카드 영역을 불러올때 /mnt/sdcard 이런 식으로 하드 코딩하시는 분들이 많은데
이런 경우 외장카드를 사용하지 않는 사용자인 경우 에러가 발생하게 됩니다.

사용자 폰에 외장카드 존재유무를 확인하여 처리하는 모듈을 간단히 구현해 봤으니 참고하세요!!

        String mSdPath;
        String ext = Environment.getExternalStorageState();
        if (ext.equals(Environment.MEDIA_MOUNTED)) {
            mSdPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
        } else {
            mSdPath = Environment.MEDIA_UNMOUNTED+"/";
        }
        File file = new File(mSdPath);
        if (!file.exists()) mSdPath = "";       

        // 파일 생성
        BufferedWriter out = new BufferedWriter(new FileWriter(mSdPath+"androes.txt"));
        out.write("test text messages");
        out.newLine();
        out.close();

        // 파일 삭제
        File file = new File(mSDPath + "/" + "androes.txt");
        file.delete();


댓글 없음:

댓글 쓰기