2013-04-03 6 views
0

Quel est le problème avec le code? J'ai déjà ajouté la permission. Logcat n'imprime pas le message qu'il est censé montrer.problème d'écriture sur la carte SD

Je suppose que je dois utiliser un flux de fichiers?

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()); 

}

Répondre

0

que vous devez faire

nom de fichier est test.xml, text.jpg ou test.txt

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

cehck ce link.

pour plus de détails sur External Storage

1

J'ai samsung galaxy s3 avec Android 4.1.2. La mémoire interne de mon téléphone s'appelle sdcard0 et ma carte externe extSdCard.

Environment.getExternalStorageDirectory() 

Ainsi, le rendement au-dessus du chemin de sdcard0 qui est la mémoire interne du téléphone

Dans ce cas, pour obtenir le chemin réel que vous pouvez utiliser le ci-dessous

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); 
} 

Une fois que vous obtenez le chemin vous pouvez utiliser le ci-dessous.

Essayez le dessous

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(); 

     } 

L'autorisation d'ajouter dans le manifeste

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />