gpt4 book ai didi

android 错误 : java. io.filenotfoundexception/proc/net/xt_qtaguid/stats + 打开失败:ENOENT(没有这样的文件或目录)

转载 作者:行者123 更新时间:2023-11-29 21:20:37 28 4
gpt4 key购买 nike

我正在尝试创建允许用户在输入文件名后保存文件的 Android 应用程序,但问题是当用户尝试保存文件时会显示错误:

 Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)

然后将文件保存为 null.txt

如何解决这个错误???

SignSactivity.java

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;
}

}

最佳答案

它抛出一个错误,因为您试图将文件保存在一个不存在的目录中。我运行了您的代码并使用断点检查了字符串 fileName 中的值.

从下面的屏幕截图中,您可以看到它是一个无效的目录,这就是问题所在。您可能想要更改目录。

enter image description here

[编辑]

更改设置文件名的方式。

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" />

关于android 错误 : java. io.filenotfoundexception/proc/net/xt_qtaguid/stats + 打开失败:ENOENT(没有这样的文件或目录),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20783594/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com