我正在创建一个android应用程序,它将创建一个文件夹,其中folderName为当前日期和时间,并且在其中将是一个文件,其中包含用户在保存笔记后将输入的文件名。
但问题是系统显示错误:打开失败的ONEONT(没有这样的文件或目录)
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;
SimpleDateFormat formatter;
// attribute for the date picker
public String fileName;
String userinputResult;
Button btn_save_soldger;
TextView txtatePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_soldger);
edit_txt_note = (EditText) findViewById(R.id.editTxtNote);
txtatePicker = (TextView) findViewById(R.id.txtDate);
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();
formatter = new SimpleDateFormat(
"yyyy-MM-dd-HH-mm-ss");
Date now = new Date();
/*
* fileName = formatter.format(now) +
* "-" + userinputResult;
*/
fileName = formatter.format(now);
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 newFolder = fileName;
String newFolder = "/" + fileName;
/*
* String sdPath = Environment.getExternalStorageDirectory()
* .getAbsolutePath() + "/" + fileName + ".txt".toString();
*/
String sdPath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/" + userinputResult + ".txt".toString();
//File myFile = new File(sdPath);
File myFolder = new File(newFolder);
myFolder.mkdirs();
File myFile = new File(myFolder + 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)
尝试写入之前,请尝试检查SD卡的状态。它可以用作共享驱动器,已损坏,已满,等等。可在此处找到状态列表:http://developer.android.com/reference/android/os/Environment.html
以下是获取状态的示例
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
然后添加此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
您确定已添加此权限以写入外部存储空间
android.permission.WRITE_EXTERNAL_STORAGE
使用此代码创建新文件夹,新文件
String sdPath = Environment.getExternalStorageDirectory().toString() + "/"+ newFolder;
//File myFile = new File(sdPath);
File myFolder = new File(sdPath);
if (!myFolder.exists()) {
myFolder.mkdirs();
}
File myFile = new File(sdPath + File.separator+fileName);
myFile.createNewFile();