마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

file 저장, 읽기관련 문의

0 추천
private String path="/storage/sdcard1/";
 private String fileName="test.txt";

private boolean fileSave() throws IOException{
  
  File file=new File(path+fileName);
  String text="sdcard Test!!";
  FileOutputStream fos = null;
  try{
   fos = new FileOutputStream(file);
   fos.write((text).getBytes());
   
   return true;
  }catch(IOException e){
   e.printStackTrace();
   Log.i("gahee", "fileSave : "+e);
   return false;
  }finally{
   if(fos!=null)
    fos.close();
  }
 }



manifast 파일

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

 

외부 sd카드에 파일을 저장하려고 합니다.

 

manifast 파일에 읽기 쓰기 권한은 있습니다.

그런데 FileNotFoundException이 뜨면서 접근권한이 없다고 뜹니다.. ㅠㅠ

 

권한을 다줬는데 왜 접근권한이 없다고뜨는건가요 ?? ㅠㅠㅠ

 

 

 

now882002 (3,860 포인트) 님이 2014년 11월 14일 질문
참고로 안드로이드 버전은 4.2 입니다..

3개의 답변

0 추천
  1. 해당 디렉토리는 만드셨나요?
aucd29 (218,390 포인트) 님이 2014년 11월 14일 답변
mkdirs() 했습니다. ㅠ
그래도 안되네요
0 추천
sd카드 경로를 저렇게 코드에 넣어놓으면 안됩니다.

sd카드 경로는 OS버전, 기기에 따라 얼마든지 바뀔 수 있습니다.

API를 사용하세요.
익명사용자 님이 2014년 11월 14일 답변
0 추천
저는 SD카드 경로 땡길때 아래와 같이 쓰고있습니다.
4.2 도 문제없내영
 
public final  String SD_CARD_PATH = getMount_sd();
 
@TargetApi(9)
public String getMount_sd() {
  List<String> mountList = new ArrayList<String>();
  String mount_sdcard = null;
 
  Scanner scanner = null;
  try {
     File vold_fstab = new File("/system/etc/vold.fstab");
     scanner = new Scanner(new FileInputStream(vold_fstab));
     while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if (line.startsWith("dev_mount") || line.startsWith("fuse_mount")) {    
           String path = line.replaceAll("\t", " ").split(" ")[2];
           if (!mountList.contains(path)){
              mountList.add(path);
           }
        }
     }
  } catch (FileNotFoundException e) {
     throw new RuntimeException(e);
  } finally {
     if (scanner != null) {
        scanner.close();
     }
  }
 
  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD){    
     if (!Environment.isExternalStorageRemovable()) {
        mountList.remove(Environment.getExternalStorageDirectory().getPath());
     }
  }
 
  for (int i = 0; i < mountList.size(); i++) {
     if (!isMounted(mountList.get(i))){
        mountList.remove(i--);
     }
  }
 
  if(mountList.size() > 0){
     mount_sdcard = mountList.get(0);
  }
   
  return mount_sdcard;
}
 
public boolean isMounted(String path) {
  boolean isMounted = false;
 
  Scanner scanner = null;
  try {
     File mounts = new File("/proc/mounts"); 
     scanner = new Scanner(new FileInputStream(mounts));
     while (scanner.hasNextLine()) {
        if (scanner.nextLine().contains(path)) {
           isMounted = true;
           break;
        }
     }
  } catch (FileNotFoundException e) {
     throw new RuntimeException(e);
  } finally {
     if (scanner != null) {
     scanner.close();
     }
  }
 
  return isMounted;
}
익명사용자 님이 2014년 11월 14일 답변
경로를 님께서 하신것처럼 바꾸어도

11-13 20:16:24.152: W/System.err(15172): java.io.IOException: open failed: EACCES (Permission denied)
11-13 20:16:24.152: W/System.err(15172):     at java.io.File.createNewFile(File.java:948)
11-13 20:16:24.152: W/System.err(15172):     at com.moimsip.hiddenmenu.SdcardTest.makeFile(SdcardTest.java:208)
11-13 20:16:24.152: W/System.err(15172):     at com.moimsip.hiddenmenu.SdcardTest.onCreate(SdcardTest.java:154)
11-13 20:16:24.152: W/System.err(15172):     at android.app.Activity.performCreate(Activity.java:5104)
11-13 20:16:24.152: W/System.err(15172):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-13 20:16:24.152: W/System.err(15172):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-13 20:16:24.152: W/System.err(15172):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-13 20:16:24.152: W/System.err(15172):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-13 20:16:24.152: W/System.err(15172):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-13 20:16:24.152: W/System.err(15172):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 20:16:24.152: W/System.err(15172):     at android.os.Looper.loop(Looper.java:137)
11-13 20:16:24.152: W/System.err(15172):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-13 20:16:24.162: W/System.err(15172):     at java.lang.reflect.Method.invokeNative(Native Method)
11-13 20:16:24.162: W/System.err(15172):     at java.lang.reflect.Method.invoke(Method.java:511)
11-13 20:16:24.162: W/System.err(15172):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-13 20:16:24.162: W/System.err(15172):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
11-13 20:16:24.162: W/System.err(15172):     at dalvik.system.NativeStart.main(Native Method)
11-13 20:16:24.162: W/System.err(15172): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
11-13 20:16:24.162: W/System.err(15172):     at libcore.io.Posix.open(Native Method)
11-13 20:16:24.162: W/System.err(15172):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-13 20:16:24.162: W/System.err(15172):     at java.io.File.createNewFile(File.java:941)
11-13 20:16:24.162: W/System.err(15172):     ... 16 more

오류가 뜨네요 ㅠㅠ
가져온 경로에 파일을 저장하는 방법?
...