Открытие файла sdcard/downloads/1.txt
Добавлено: 22 май 2014, 15:29
Здравствуйте, помогите пожалуйста решить проблему, вообщем есть проект состоящий из нескольких активити. Первая активити fileManager позволяет выбрать файл и передает его на другую активити, которая должна выводить содержимое файла на экран в editText окно. Вообщем вот код
FileManager
graphContent(активити которая открывает файл на чтение)
Но открыть не получается, выдается ошибка "file/sdcard/download/1.txt contain a path separator"
Я так понимаю косяк в этой строке
строка в манифест добавлена
Что не так я делаю?
FileManager
Код: Выделить всё
public class FileManager extends ListActivity {
private List<String> directoryEntries = new ArrayList<String>();
private File currentDirectory = new File("/");
//when application started
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//set main layout
setContentView(R.layout.activity_file_manager);
//browse to root directory
browseTo(new File("/"));
}
//browse to parent directory
private void upOneLevel(){
if(this.currentDirectory.getParent() != null) {
this.browseTo(this.currentDirectory.getParentFile());
}
}
//browse to file or directory
private void browseTo(final File aDirectory){
//if we want to browse directory
if (aDirectory.isDirectory()){
//fill list with files from this directory
this.currentDirectory = aDirectory;
fill(aDirectory.listFiles());
//set titleManager text
TextView titleManager = (TextView) findViewById(R.id.titleManager);
titleManager.setText(aDirectory.getAbsolutePath());
} else {
//if we want to open file, show this dialog:
//listener when YES button clicked
OnClickListener okButtonListener = new OnClickListener(){
public void onClick(DialogInterface arg0, int arg1) {
//intent to navigate file
Intent i = new Intent(FileManager.this, graph_content.class);
i.putExtra("path", aDirectory.getAbsolutePath());
startActivity(i);
}
};
//listener when NO button clicked
OnClickListener cancelButtonListener = new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do nothing
//or add something you want
}
};
//create dialog
new AlertDialog.Builder(this)
.setTitle("Подтверждение") //title
.setMessage("Хотите открыть файл "+ aDirectory.getName() + "?") //message
.setPositiveButton("Да", okButtonListener) //positive button
.setNegativeButton("Нет", cancelButtonListener) //negative button
.show(); //show dialog
}
}
//fill list
private void fill(File[] files) {
//clear list
this.directoryEntries.clear();
if (this.currentDirectory.getParent() != null)
this.directoryEntries.add("..");
//add every file into list
for (File file : files) {
this.directoryEntries.add(file.getAbsolutePath());
}
//create array adapter to show everything
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, R.layout.activity_row, this.directoryEntries);
this.setListAdapter(directoryList);
}
//when you clicked onto item
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected file name
int selectionRowID = position;
String selectedFileString = this.directoryEntries.get(selectionRowID);
//if we select ".." then go upper
if(selectedFileString.equals("..")){
this.upOneLevel();
} else {
//browse to clicked file or directory using browseTo()
File clickedFile = null;
clickedFile = new File(selectedFileString);
if (clickedFile != null)
this.browseTo(clickedFile);
}
}
}Код: Выделить всё
public class graph_content extends Activity {
private EditText mEdit; // текстовое поле
private String filepath; //
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
mEdit = (EditText) findViewById(R.id.editText);
if (intent != null && intent.hasExtra("path")) {
filepath = intent.getStringExtra("path");
} else {
finish();
}
openFile(filepath);
}
// Метод для открытия файла
private void openFile(String fileName) {
try {
InputStream inputstream = openFileInput(fileName);
if (inputstream != null) {
InputStreamReader isr = new InputStreamReader(inputstream);
BufferedReader reader = new BufferedReader(isr);
String str;
StringBuffer buffer = new StringBuffer();
while ((str = reader.readLine()) != null) {
buffer.append(str + "\n");
}
inputstream.close();
mEdit.setText(buffer.toString());
}
} catch (Throwable t) {
Toast.makeText(getApplicationContext(),
"Exception: " + t.toString(), Toast.LENGTH_LONG).show();
}
}
Я так понимаю косяк в этой строке
Код: Выделить всё
InputStream inputstream = openFileInput(fileName);Код: Выделить всё
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>