我现在可以在Google Glass中安装原生应用。我希望知道如何使用Google Glass中的原生应用创建文本文件,以便将数据保存到文本文件中。
谢谢!
答案 0 :(得分:1)
由于Glass是Android设备,并且您正在为其编写原生Android应用程序,因此您应该能够像在任何其他Android设备上一样编写文本文件。
答案 1 :(得分:1)
我尝试使用以下代码在Google Glass中创建文件。以下代码适用于Android,它有一张SD卡。但对Google Glass无效。有谁知道如何在Glass中创建文本文件?谢谢!
public void SaveSubjectID(String filename, Context ctx) {
try {
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
// Toast.makeText(getBaseContext(), "files", Toast.LENGTH_SHORT).show();
//create a folder in a app.
//http://stackoverflow.com/questions/6911041/how-to-create-folder-into-sd-card-in-android
File rootDirect = new File(Environment.getExternalStorageDirectory() + "/SmartTexting");
if(!rootDirect.exists())
{ rootDirect.mkdir() ;
}
File subjectDirect = new File(Environment.getExternalStorageDirectory() + "/SmartTexting/"+"subject");
if(!subjectDirect.exists())
{ subjectDirect.mkdir() ;
}
//File sdcard = Environment.getExternalStorageDirectory();
File file = new File(subjectDirect,filename);
// Toast.makeText(getBaseContext(), "files created", Toast.LENGTH_SHORT).show();
FileOutputStream fos2 = new FileOutputStream(file, true);
String outline = "test"
+ "\n";
fos2.write(outline.getBytes());
fos2.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 2 :(得分:0)
我希望你现在已经找到了正确答案,但这个答案适用于所有尚未找到答案的人。
将文件写入谷歌眼镜的代码:
try {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File f = new File(path,"file_new.txt");
f.createNewFile();
Log.d("file pathhhh", "result"+f.getAbsolutePath());
Log.d("file created", "result"+f.createNewFile());
FileOutputStream fOut = new FileOutputStream(f);
//FileOutputStream fileout=openFileOutput(f);
OutputStreamWriter outputWriter=new OutputStreamWriter(fOut);
//writer = new FileWriter(file1);
//writer.write(text.toString());
outputWriter.write(text.toString());
/** Closing the writer object */
outputWriter.close();
Log.d("success", "success"+Environment.getExternalStorageState()+Environment.getStorageState(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
关键是在DCIM文件夹中写入文件,不要指望只创建文件。
要查看文件是否已创建,请关闭玻璃板并再次打开电源,您将在DCIM文件夹中找到新文件