0

XMLファイルをデバイスに書き込むメソッドに取り組んでいます。マニフェストファイルで外部ストレージを許可しましたが、本来あるべき場所にファイルが見つかりません。

これが私のコードです:

 public static void write (){   
 Serializer serial = new Persister();
 File sdcardFile = new File("/Prueba/file.xml");
    Item respuestas = new Item();

   try {
 serial.write(respuestas, 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());
   }

   }
4

1 に答える 1

1

SDカードファイルのパスの問題。これは、file.xmlファイルに文字列を書き込む例です。

File myFile = new File("/sdcard/file.xml");



try {
            File myFile = new File("/sdcard/file.xml");
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter = 
                                    new OutputStreamWriter(fOut);
            myOutWriter.append("encodedString");
            myOutWriter.close();
            fOut.close();

        } catch (Exception e) {

        }

この方法で外部ストレージ名を取得できます。

String root = Environment.getExternalStorageDirectory().toString();
于 2013-02-26T09:38:16.433 に答える