stopWriting = (Button) findViewById(R.id.save);
stopWriting.setOnClickListener(new OnClickListener() {
@SuppressLint("SdCardPath")
public void onClick(View v) {
// stop recording the sensor data
try {
myFile = new File("/sdcard/SensorData/" + txtData.getText() + ".txt");
myFile.createNewFile();
sData = new FileOutputStream(myFile);
myOutWriter = new OutputStreamWriter(sData);
myBufferedWriter = new BufferedWriter(myOutWriter);
myPrintWriter = new PrintWriter(myBufferedWriter);
//if(myFile != null )//stopFlag = mSensorManager.cancelTriggerSensor(null, null);
Toast.makeText(getBaseContext(), "Done", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
我正在尝试将陀螺仪数据保存到txt文件,但它不能保存。如果有人发现问题,请帮我纠正。
答案 0 :(得分:31)
尝试使用此代码为我工作。
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
别忘了取得许可
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:13)
试试这段代码:
public void writeToFile(String fileName, String body)
{
FileOutputStream fos = null;
try {
final File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/folderName/" );
if (!dir.exists())
{
if(!dir.mkdirs()){
Log.e("ALERT","could not create the directories");
}
}
final File myFile = new File(dir, fileName + ".txt");
if (!myFile.exists())
{
myFile.createNewFile();
}
fos = new FileOutputStream(myFile);
fos.write(body.getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
另外,请记住在清单文件中包含外部存储权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并在Android 6.0上请求WRITE_EXTERNAL_STORAGE
的许可答案 2 :(得分:1)
您是否曾使用权限写入sd?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 3 :(得分:0)
我这样做
public void write()
{
FileOutputStream output = null;
try
{
String mediaState = Environment.getExternalStorageState();
if(mediaState.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
{
output = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/download/shared.txt"));
String str = "Hello, writing to a text file";
byte[] b = encrypt2(str);
output.write(b);
}
else
{
// error
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (output != null)
output.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static byte[] encrypt2(String value)
{
byte[] input = value.getBytes();
int nLen = input.length;
if(nLen % 2 != 0)
{
nLen--;
}
byte temp;
for (int i = 0; i < nLen; i += 2)
{
temp = input[i+1];
input[i+1] = input[i];
input[i] = temp;
}
// do encryption
return Base64.encode(input, Base64.NO_WRAP);
}
此外,还需要此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 4 :(得分:0)
试试这个..
stopWriting.setOnClickListener(new OnClickListener() {
@SuppressLint("SdCardPath")
public void onClick(View v) {
// stop recording the sensor data
try {
myFile = new File("/sdcard/SensorData/" + txtData.getText() + ".txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD " + txtData.getText() + ".txt",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
以及清单文件中的外部存储权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 5 :(得分:0)
首先向androidManifest.xml声明此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
然后在按钮点击事件上编写此代码
try {
putStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
myOutWrite File myFile = new File("/sdcard/SensorData.txt");
myFile.createNewFile();
FileOutr.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(v.getContext(),"Done writing SD 'SensorData.txt'", Toast.LENGTH_SHORT).show();
txtData.setText("");
}
catch (Exception e)
{
Toast.makeText(v.getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
}
}