2014-09-11 1 views
1

Так что я пытаюсь в основном написать несколько журналов в текстовый файл, чтобы потом просмотреть его. Я запускаю это на физическом телефоне, а не на эмуляторе. Я пробовал так много разных вариаций, и, насколько я понял, он записывал данные/данные и хранилище/эмулировал, но я никогда не могу получить доступ к моему файлу. Любая помощь будет оценена по достоинству. Некоторые из моих последних неудаленных примеров были:Написание текстового файла в хранилище android для просмотра позже

String filename = "myfile.txt"; 
try { 
    File path = new File(context.getFilesDir(), "myfolder"); 
    if (!path.exists()) 
     path.mkdir(); 
    File output = new File(path, filename); 
    BufferedWriter buff = new BufferedWriter(new FileWriter(output)); 
    buff.append("hi"); 
    Log.d("Success", 
      "Successfully wrote a file " + context.getFilesDir()); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

и

final String appPath = String.format("%s/Datafiles", 
     context.getFilesDir()); 
File path = new File(appPath); 
if (!path.exists()) { 
    path.mkdir(); 
} 
String fileName = String.format("%s/filedemo.txt", appPath); 

и

try { 
    String filename = "myfile.txt"; 
    File path = new File(Environment.getRootDirectory(), "myfolder"); 
    if (!path.exists()) 
     path.mkdir(); 
    File output = new File(path, filename); 
    BufferedWriter buff = new BufferedWriter(new FileWriter(output)); 
    buff.append("hi"); 
    Log.d("Success", 
      "Successfully wrote a file " + context.getFilesDir()); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

Оба следующих положить его в/хранение/эмулировать/0/Android/данные/com.example.simplelte/files/system/stuff/test.txt и /storage/emulated/0/Android/data/com.example.simplelte/files/storage/emulated/0/stuff/test.txt соответственно

try { 
    File file = new File(context.getExternalFilesDir(Environment 
      .getRootDirectory().getCanonicalPath()), "stuff"); 
    if (!file.mkdirs()) { 
     Log.e(LOG_TAG, "Directory not created"); 
    } 
    File output = new File(file, "test.txt"); 
    BufferedWriter buff; 
    buff = new BufferedWriter(new FileWriter(output)); 
    buff.append("hi"); 
    buff.close(); 
    Log.d("Success", output.getCanonicalPath()); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

и

try { 
    File file = new File(context.getExternalFilesDir(Environment 
      .getExternalStorageDirectory().getCanonicalPath()), 
      "stuff"); 
    if (!file.mkdirs()) { 
     Log.e(LOG_TAG, "Directory not created"); 
    } 
    File output = new File(file, "test.txt"); 
    BufferedWriter buff; 
    buff = new BufferedWriter(new FileWriter(output)); 
    buff.append("hi"); 
    buff.close(); 
    Log.d("Success", output.getCanonicalPath()); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

и многое другое. Я следил за каждым примером, о котором я могу думать. Я буквально просто хочу просмотреть текстовый файл в разделе Computer \ Nexus 5 \ Internal storage \ Я просто простой человек с простыми желаниями. Почему это должно быть так сложно.

ответ

0

Вы пробовали это?

File sdCard = Environment.getExternalStorageDirectory(); 
File dir = new File (sdCard.getAbsolutePath() + "/"); 
File file = new File(dir, "text.txt"); 

FileOutputStream f = new FileOutputStream(file); 
+0

просто попробовал. Невозможно просмотреть файл в проводнике. Создает /storage/emulated/0/text.txt – Ion