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();
}
}
});
I am trying to save gyroscope data to txt file but it does not save. If anybody finds the problem, please help to me correct it.
我正在尝试将陀螺仪数据保存到txt文件中,但它没有保存。如果有人发现这个问题,请帮助我改正。
6 个解决方案
#1
30
try this code its working for me.
试试这段代码为我工作。
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();
}
dont forget to take permission
别忘了征得同意
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#2
13
Try this code:
试试这段代码:
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();
}
}
Also, remember to include the external storage permission in your manifest file:
另外,请记住在清单文件中包括外部存储权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and on android 6.0 to ask for permission for WRITE_EXTERNAL_STORAGE
在android 6.0上请求WRITE_EXTERNAL_STORAGE权限
#3
1
Have you used permission to write in sd?
你是否曾使用权限来写sd文件?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#4
0
I am doing it like this
我这样做。
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);
}
Also this permission is required
同时也需要这个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#5
0
Try this..
试试这个. .
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();
}
}
});
and also external storage permission in your manifest file:
以及您的清单文件中的外部存储权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#6
0
First Declare this permission to your androidManifest.xml:
首先向您的androidManifest.xml声明此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then write this code on your button click events
然后在单击事件按钮上编写此代码
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();
}
}
#1
30
try this code its working for me.
试试这段代码为我工作。
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();
}
dont forget to take permission
别忘了征得同意
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#2
13
Try this code:
试试这段代码:
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();
}
}
Also, remember to include the external storage permission in your manifest file:
另外,请记住在清单文件中包括外部存储权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and on android 6.0 to ask for permission for WRITE_EXTERNAL_STORAGE
在android 6.0上请求WRITE_EXTERNAL_STORAGE权限
#3
1
Have you used permission to write in sd?
你是否曾使用权限来写sd文件?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#4
0
I am doing it like this
我这样做。
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);
}
Also this permission is required
同时也需要这个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#5
0
Try this..
试试这个. .
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();
}
}
});
and also external storage permission in your manifest file:
以及您的清单文件中的外部存储权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
#6
0
First Declare this permission to your androidManifest.xml:
首先向您的androidManifest.xml声明此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then write this code on your button click events
然后在单击事件按钮上编写此代码
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();
}
}