0

代码有什么问题?我已经添加了权限。Logcat 没有打印它应该显示的消息。

我猜我必须使用文件流?

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

}

4

3 回答 3

1

我有android 4.1.2的三星 Galaxy s3 。我的内部手机内存命名为 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
}
于 2013-04-03T09:07:08.617 回答
0

你所要做的

文件名是 test.xml、text.jpg 或 test.txt

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

检查此链接

有关外部存储的更多详细信息

于 2013-04-03T09:06:58.580 回答
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" />
于 2013-04-03T09:26:09.880 回答