我想保存文件到SD卡中的文本,在下面的一些教程後,我得到一個關於我應該使用的編碼的參考。 但我運行後它不起作用,只是得到消息「保存的文件失敗」。 有沒有我的編碼錯誤? 我想在活動中保存來自textview的文件。將文本文件保存到SD卡
我還對的manifest.xml使用此代碼
public class PreposisiRemoval extends Activity implements OnClickListener{
Button button;
Button buttonsave;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preposisi_removal);
Intent intent = getIntent();
String result = intent.getStringExtra("resultFilter");
tv = (TextView) findViewById(R.id.textView8);
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setText(result);
tv.setTextSize(14);
buttonsave = (Button) findViewById(R.id.button8);
buttonsave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
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;
}
try {
File myFile = new File("/sdcard/documents/PreposisiRemoval.txt");
myFile.createNewFile();
FileOutputStream FOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(FOut);
myOutWriter.append(tv.getText());
myOutWriter.close();
FOut.close();
Toast.makeText(getBaseContext(), "Save Files Successful", Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(getBaseContext(), "Save Files Failed", Toast.LENGTH_SHORT).show();
}
}
});
其中位於所述問題添加消息
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
IAM?
謝謝先生,讓我試試我會報告結果 – abooo
不成功先生 我一直在關注你的命令 – abooo
那麼,什麼是堆棧跟蹤? –