java.lang.IllegalArgumentException : 경로 구분 기호 포함
내 코드에 다음과 같은 파일 이름이 있습니다.
String NAME_OF_FILE="//sdcard//imageq.png";
FileInputStream fis =this.openFileInput(NAME_OF_FILE); // 2nd line
두 번째 줄에 오류가 발생합니다.
05-11 16 : 49 : 06.355 : ERROR / AndroidRuntime (4570) : 원인 : java.lang.IllegalArgumentException : 파일 //sdcard//imageq.png에 경로 구분 기호가 있습니다.
이 형식도 시도했습니다.
String NAME_OF_FILE="/sdcard/imageq.png";
이 메서드는 응용 프로그램의 개인 데이터 영역에서 파일을 엽니 다. 이 방법을 사용하여이 영역 또는 완전히 다른 영역의 하위 디렉터리에있는 파일을 열 수 없습니다. 따라서의 생성자를 FileInputStream
직접 사용하여 디렉토리가있는 경로를 전달하십시오.
해결책은 다음과 같습니다.
FileInputStream fis = new FileInputStream (new File(NAME_OF_FILE)); // 2nd line
openFileInput 메서드는 경로 구분 기호를 허용하지 않습니다.
잊지 마세요
fis.close();
끝에.
openFileInput()
경로를 허용하지 않고 경로에 액세스하려는 경우 파일 이름 만 사용 File file = new File(path)
하고 해당FileInputStream
디렉토리 구분 기호가있는 경로를 직접 사용할 수는 없지만 모든 디렉토리에 대해 파일 객체를 만들어야합니다.
참고 :이 코드는 디렉토리를 생성하므로 필요하지 않을 수 있습니다.
File file= context.getFilesDir();
file.mkdir();
String[] array=filePath.split("/");
for(int t=0; t< array.length -1 ;t++)
{
file=new File(file,array[t]);
file.mkdir();
}
File f=new File(file,array[array.length-1]);
RandomAccessFileOutputStream rvalue = new RandomAccessFileOutputStream(f,append);
openFileInput("/Dir/data.txt")
하위 디렉터리가있는 메서드를 사용하여 내부 저장소에서 파일에 액세스하는 동안 위의 오류 메시지가 나타납니다 Dir
.
위의 방법으로는 하위 디렉터리에 액세스 할 수 없습니다.
다음과 같이 시도하십시오.
FileInputStream fIS = new FileInputStream (new File("/Dir/data.txt"));
String all = "";
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
String strLine;
while ((strLine = br.readLine()) != null){
all = all + strLine;
}
} catch (IOException e) {
Log.e("notes_err", e.getLocalizedMessage());
}
File file = context.getFilesDir();
file.mkdir();
String[] array = filePath.split("/");
for(int t = 0; t < array.length - 1; t++) {
file = new File(file, array[t]);
file.mkdir();
}
File f = new File(file,array[array.length- 1]);
RandomAccessFileOutputStream rvalue =
new RandomAccessFileOutputStream(f, append);
onCreate 이벤트에서 디렉터리를 만든 다음 해당 디렉터리에서 파일을 저장하거나 검색하는 등의 작업을 수행해야하는 메서드에서 새 파일 개체를 만들어 디렉터리에 액세스하여 이러한 유형의 오류를 해결했습니다. 도움이 되었기를 바랍니다.
public class MyClass {
private String state;
public File myFilename;
@Override
protected void onCreate(Bundle savedInstanceState) {//create your directory the user will be able to find
super.onCreate(savedInstanceState);
if (Environment.MEDIA_MOUNTED.equals(state)) {
myFilename = new File(Environment.getExternalStorageDirectory().toString() + "/My Directory");
if (!myFilename.exists()) {
myFilename.mkdirs();
}
}
}
public void myMethod {
File fileTo = new File(myFilename.toString() + "/myPic.png");
// use fileTo object to save your file in your new directory that was created in the onCreate method
}
}
'programing tip' 카테고리의 다른 글
Ruby에서 하나의 배열을 다른 배열에서 빼기 (0) | 2020.10.14 |
---|---|
최대는 얼마입니까? (0) | 2020.10.14 |
ASP.net MVC 4에서 부분보기 사용 (0) | 2020.10.14 |
Volley JsonObjectRequest Post 요청이 작동하지 않습니다. (0) | 2020.10.14 |
Fowler의 "엔터프라이즈 애플리케이션 아키텍처 패턴"은 여전히 관련이 있습니까? (0) | 2020.10.14 |