1
我是Android編程的新手,爲我的畢業論文編寫我的第一個應用程序。 現在我幾乎完成了,但還剩一件事,我無法在網上的任何地方得到答案。所以也許這裏有人可以幫忙。如何將數據寫入文件並從應用程序外部訪問(無SD卡)?
我的應用程序必須做的一部分是將以前操作的結果寫入可自由訪問的文件以供日後分析。 所以我走到這一步,是我能夠寫一個文件到SD卡用下面的代碼:
String packageName = this.getClass().getPackage().getName();
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + packageName + "/files/";
if (isExternalStorageAvailable() && isExternalStorageWritable())
{
String filename = "_results.dat";
boolean exists = (new File(path)).exists();
if (!exists)
{
new File(path).mkdirs();
}
// Open output stream
FileOutputStream fOut = new FileOutputStream(path + filename,true);
fOut.write("lalala".getBytes());
etc.
}
public static boolean isExternalStorageAvailable()
{
// Retrieving the external storage state
String state = Environment.getExternalStorageState();
// Check if available
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{
return true;
}
return false;
}
public static boolean isExternalStorageWritable()
{
// Retrieving the external storage state
String state = Environment.getExternalStorageState();
// Check if writable
if (Environment.MEDIA_MOUNTED.equals(state))
{
return true;
}
return false;
}
該文件以後可以訪問,複製,等等。就像我想要的那樣。現在的實際問題是:
有沒有辦法實現這一目標,而不在手機的SD卡?或者我只能寫入「應用程序內存」(這不能滿足我的需求)? 我只問,因爲目前還沒有SD卡在我要部署應用程序的手機中。我們必須決定是否必須'升級'設備:)。
SD卡或其他外部存儲是非常普遍的Android手機。如果不知道,我會說,所有Android手機中的90%都有外部存儲。 –