我正在尝试创建Android应用程序,允许用户在输入文件名后保存文件,但问题是当用户尝试保存文件时会显示错误,说明:
Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)
然后保存为 null.txt
的文件如何修复此错误???
package com.devleb.idapp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SignSoldgerActivity extends Activity {
EditText edit_txt_note;
final Context context = this;
// attribute for the date picker
public String fileName;
String userinputResult;
Button btndatePicker, btn_save_soldger;
TextView txtatePicker;
int year, monthofyear, dayofmonth;
Calendar cal = Calendar.getInstance();
DateFormat dt = DateFormat.getInstance();
DatePickerDialog.OnDateSetListener dpd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_soldger);
edit_txt_note = (EditText) findViewById(R.id.editTxtNote);
btndatePicker = (Button) findViewById(R.id.btnDateTimePicker);
btndatePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new DatePickerDialog(SignSoldgerActivity.this, dpd, cal
.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal
.get(Calendar.DAY_OF_MONTH)).show();
}
});
txtatePicker = (TextView) findViewById(R.id.txtDate);
dpd = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
txtatePicker.setText(new StringBuilder().append(year + "/")
.append(monthOfYear + "/").append(dayOfMonth));
}
};
btn_save_soldger = (Button) findViewById(R.id.btnSaveSoldger);
btn_save_soldger.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// / for creating a dialog
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// get user input and set it to result
// edit text
userinputResult = userInput.getText()
.toString();
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy/MM/dd\\HH:mm:ss");
Date now = new Date();
fileName = formatter.format(now) + "/"
+ userinputResult;
saveFile(fileName);
txtatePicker.setText(fileName);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
// / for saving the file on the SD
public void saveFile(String fileName) {
try {
String sdPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/" + fileName + ".txt";
File myFile = new File(sdPath);
myFile.createNewFile();
Toast.makeText(getBaseContext(), "the second step in saving file",
Toast.LENGTH_SHORT).show();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
// append or write
myOutWriter.append(edit_txt_note.getText());
myOutWriter.close();
fOut.close();
edit_txt_note.setText("");
Toast.makeText(getBaseContext(), "Done Writing SD" + fileName,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sign_soldger, menu);
return true;
}
}
答案 0 :(得分:2)
它正在抛出一个错误,因为您正在尝试将文件保存在不存在的目录中。我运行你的代码并使用断点我检查字符串fileName
中的值。
从下面的屏幕截图中您可以看到它是一个无效的目录,问题出在哪里。您可能想要更改目录。
<强> [编辑] 强>
更改您设置文件名称的方式。
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Date now = new Date();
fileName = formatter.format(now) + "-" + userinputResult;
saveFile(fileName);
现在,fileName
将有当前日期+用户输入的文字。
例如,文件名将为2013-12-26-18-51-26-hello.txt
另外,请确保您已添加权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />