파일을 선택하려면 파일 브라우저를 사용자에게 제공하고 있습니다. onActivityResult에서 파일 경로를 - /file/sdcard/Android/data/com.coca_cola.android.conferenceapp/cache/Conference/export.txt로 가져 왔습니다. 이 파일 개체를 만들려고 할 때 만들 수 없습니다. 제가/파일을 제거/sdcard/안드로이드/데이터/com.coca_cola.android.conferenceapp/cache/Conference/export.txt에서 파일 객체를 생성하면 생성됩니다. 하지만 하드 코드를 제거/파일 /에서 다른 경로로 다른 경로를 제공 할 것입니다 파일 경로에서. 아래의 코드파일 객체가 생성되지 않음
private void readContactFromFile(String path) {
StringBuilder text = new StringBuilder();
try {
String st = "/file/sdcard/Android/data/com.coca_cola.android.conferenceapp/cache/Conference/export.txt";
File file = new File(st);
if (file.exists()) {
Log.v("TTT", "file exist");
}
Log.v("TTT", file.toString());
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
Log.i("Test", "text : " + text + " : end");
text.append('\n');
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
내가 onActivityResult를
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_PICK_FILE:
Uri uri = data.getData();
String path = data.getData().getPath();
Log.v("PATH", path);
readContactFromFile(path);
break;
}
}
}
패스를 얻고있다 그리고 나는 완벽한 경로를 얻을 수있는 방법 파일 브라우저
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*"); //all files
//intent.setType("text/xml"); //XML file only
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), REQUEST_PICK_FILE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
을 개방을 요구하고 어떻게입니다 또는이 문제를 어떻게 해결할 수 있습니까? 미리 감사드립니다.
질문을 편집하고이 onActivityResult() 호출을 시작하는'startActivityForResult()'호출을 게시하십시오. – CommonsWare
나는 그랬다. 체크인시 확인하십시오 –