写到SD卡的麻烦

时间:2013-04-03 09:02:02

标签: android file-io

代码有什么问题?我已经添加了许可。 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());

}

3 个答案:

答案 0 :(得分:1)

我用android 4.1.2 samsung 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
}

答案 1 :(得分:0)

你必须这样做

filename是test.xml,text.jpg或test.txt

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

cehck this link

了解有关External Storage

的更多详细信息

答案 2 :(得分: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" />