如何解决Android Studio:如何保存到文件
我正在执行此代码以将一些数据保存到我的 android 手机中的文件中,但是当保存它时 FileNotFoundException 触发,我不知道为什么,因为我从许多不同的地方复制但不适用于我。
提示?
public void safeToExcelFile(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1000);
}
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
String fileName = "hola" + ".txt";
String contenido = "pues algo a ver si va";
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS);
File dir = new File(root.getAbsolutePath()+"/MyAppFile");
if(!dir.exists()) {
dir.mkdir();
}
File file = new File(dir,fileName);
Toast.makeText(this,dir.getPath(),Toast.LENGTH_LONG).show();
try{
FileOutputStream fos = new FileOutputStream(file);
fos.write(contenido.getBytes());
fos.close();
Toast.makeText(this,"Se guardo",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Toast.makeText(this,"Archivo no encontrado",Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this,"Error al guardar",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}else{
Toast.makeText(this,"No puedes guardar",Toast.LENGTH_LONG).show();
}
}
解决方法
托马赫曼诺
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1000);
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
String fileName = "hola" + ".txt";
String contenido = "pues algo a ver si va";
String path = getExternalFilesDir(null) + "/" + fileName;
try{
FileOutputStream fos = new FileOutputStream(path);
fos.write(contenido.getBytes());
fos.close();
Toast.makeText(this,"Se guardo",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Toast.makeText(this,"Archivo no encontrado",Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this,"Error al guardar",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}else{
Log.d("Error","No hay permiso");
}
Haz un log del path y verás dónde se guarda el fichero。 Es el satio Correcto para almacenar ficheros locales。
,我终于找到了答案,如果您有兴趣,我从 https://blog.cindypotvin.com/saving-data-to-a-file-in-your-android-application/ 那里得到了解决方案
try {
File testFile = new File(this.getExternalFilesDir(null),"Cuentas.csv");
if (!testFile.exists())
testFile.createNewFile();
Toast.makeText(this,testFile.getPath(),Toast.LENGTH_LONG).show();
// Adds a line to the file
BufferedWriter writer = new BufferedWriter(new FileWriter(testFile,true));
writer.write("This is a test file.");
writer.close();
// Refresh the data so it can seen when the device is plugged in a
// computer. You may have to unplug and replug the device to see the
// latest changes. This is not necessary if the user should not modify
// the files.
MediaScannerConnection.scanFile(this,new String[]{testFile.toString()},null,null);
} catch (IOException e) {
//Log.e("ReadWriteFile","Unable to write to the TestFile.txt file.");
Toast.makeText(this,Toast.LENGTH_LONG).show();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。