我目前正在使用适用于Android的Notes应用程序。 问题:我不知道如何保存txt。我的内部存储上的应用程序文件。我已经用外部存储编码了它。这是我的代码(MainActivity.java)
`package de.tkdevelopment.notizen;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
Button btn;
EditText editText;
File ordner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ordner = new File(Environment.getExternalStorageDirectory(), "NotizenApp"); //Ordnername
if(!ordner.exists()){
ordner.mkdirs();
}
editText = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText.getText().length() > 0){ //Wenn Text länger als 0 dann Spiechern
File notizdatei = new File(ordner, "Notiz_" + System.currentTimeMillis() + ".txt");
try {
OutputStream outputStream = new FileOutputStream(notizdatei);
outputStream.write(editText.getText().toString().getBytes());
outputStream.close();
editText.setText(null);
Toast.makeText(getApplicationContext(), "Notiz gespeichert", Toast.LENGTH_LONG);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else { //Sonst nicht Speichern
Toast.makeText(getApplicationContext(), "Kein Text vorhanden", 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.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_view_notes) {
if(ordner.listFiles().length > 0){
startActivity(new Intent(MainActivity.this, Notizen_auslesen.class));
} else {
Toast.makeText(getApplicationContext(), "Keine Notizen vorhanden", Toast.LENGTH_SHORT).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
`
答案 0 :(得分:0)
将此方法添加到您的活动中:
public String storeToInternalStorage(Context context,String data) {
ContextWrapper cw = new ContextWrapper(context);
// path to /data/data/yourapp/app_data/PrivateFiles
File directory = cw.getDir("PrivateFiles", Context.MODE_PRIVATE);
File file = new File(directory, "filename.txt");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data.getBytes());
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return file.getAbsolutePath();
}
然后像这样使用它:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText.getText().length() > 0){
String data = editText.getText().toString().trim();
String fileStoredPath = storeToInternalStorage(this, data);
Log.d("TAG","File is stored at:" + fileStoredPath);
}
});