2013-04-03 3 views
0

Что не так с кодом? Я уже добавил разрешение. Logcat не печатает сообщение, которое должно показать.Проблема с записью на SDCard

Я предполагаю, что мне нужно использовать филе?

public class Run { 

int abc = 2; 
int[] myIntArray = {1,2,3}; 
String texts = "abcabac"; 

//Person p = new Person(); 
Paragraph p = new Paragraph(abc, texts, myIntArray); 


Serializer serializer = new Persister(); 
File file = new File("paragraphs.xml"); 




private final static String TAG = Run.class.getCanonicalName(); 

String a = "writeing something nothing"; 

    // Now write the level out to a file 

    Serializer serial = new Persister(); 

    //File sdDir = Environment.getExternalStorageDirectory(); should use this?? 

    //File sdcardFile = new File("/sdcard/paragraphs.xml"); 
    File sdcardFile = new File(Environment.getExternalStorageDirectory().getPath()); 
    { 

    try { 
     serial.write(p, sdcardFile); 
    } catch (Exception e) { 
     // There is the possibility of error for a number of reasons. Handle this appropriately in your code 
     e.printStackTrace(); 
    } 
    Log.i(TAG, "XML Written to File: " + sdcardFile.getAbsolutePath()); 

}

ответ

0

вы должны сделать

имя файла является test.xml, text.jpg или test.txt

File sdcardFile = new File(Environment.getExternalStorageDirectory()+"/"+filename); 

cehck это link.

подробнее о External Storage

1

У меня есть Samsung Galaxy s3 с андроид 4.1.2. Внутренняя память телефона называется sdcard0 и моей внешней картой extSdCard.

Environment.getExternalStorageDirectory() 

Таким образом, выше возвращает путь sdcard0 который является память внутренней телефонной

В таких случаях, чтобы получить фактический путь, который вы можете использовать ниже

String externalpath = new String(); 
String internalpath = new String(); 

public void getExternalMounts() { 
Runtime runtime = Runtime.getRuntime(); 
try 
{ 
Process proc = runtime.exec("mount"); 
InputStream is = proc.getInputStream(); 
InputStreamReader isr = new InputStreamReader(is); 
String line; 
BufferedReader br = new BufferedReader(isr); 
while ((line = br.readLine()) != null) { 
if (line.contains("secure")) continue; 
if (line.contains("asec")) continue; 

if (line.contains("fat")) {//external card 
    String columns[] = line.split(" "); 
    if (columns != null && columns.length > 1) { 
     externalpath = externalpath.concat("*" + columns[1] + "\n"); 
    } 
} 
    else if (line.contains("fuse")) {//internal storage 
    String columns[] = line.split(" "); 
    if (columns != null && columns.length > 1) { 
     internalpath = internalpath.concat(columns[1] + "\n"); 
    } 
    } 
    } 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
    System.out.println("Path of sd card external............"+externalpath); 
    System.out.println("Path of internal memory............"+internalpath); 
} 

После того, как вы получите путь вы можете использовать ниже.

Попробуйте ниже

String filename = "filename.xml"; 
File file = new File(Environment.getExternalStorageDirectory(), filename); 
//Instead of Environment.getExternalStorageDirectory() you can use internalpath or externalpath from the above code. 
FileOutputStream fos; 
byte[] data = new String("data to write to file").getBytes(); 
try { 
fos = new FileOutputStream(file); 
fos.write(data); 
fos.flush(); 
fos.close(); 
} catch (FileNotFoundException e) { 
// handle exception 
} catch (IOException e) { 
// handle exception 
} 
0
//to get sdcard path 
    String sdcardpath = Environment.getExternalStorageDirectory().getPath(); 

    //to write a file in sd card 
    File file = new File("/sdcard/FileName.txt"); 
    if (!file.exists()) { 
     file.mkdirs(); 

     } 

Разрешение добавить в очевидном

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Смежные вопросы