Android個人本地隨筆記事本開發
阿新 • • 發佈:2021-01-18
技術標籤:Androidandroidnotelistfiles
概述
想要隨手寫點東西感想之類的,或者一些臨時的個人小心思,祕密啥的,又怕聯網不安全,寫一個純本地的檔案型別的小記事本,無資料庫之類的亂七八糟的。也沒有什麼什麼許可權要去申請。
先上圖
取得本地檔案列表
private void getFileList() {
File rootDir = new File(rootPath);
if (!rootDir.exists()) {
//android高版本都要動態申請許可權,現在手機基本都是android5之後了
if (Build. VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
}
}
lstFile.clear();
File[] fList = rootDir.listFiles();
if(fList != null && fList.length > 0){
for (File file : fList){
if(file.isDirectory()) continue;
String suffix = file.getName().substring(file.getName().lastIndexOf('.'));
if(suffix.equals(".zgn") || suffix.equals(".txt")){
String filename = file.getName().substring(0, file. getName().lastIndexOf('.'));
lstFile.add(new FileInfoModule(filename, file.getAbsolutePath(), file.lastModified()));
}
}
Collections.sort(lstFile, (fileInfoModule, t1) -> String.valueOf(t1.getLastModified()).compareTo(String.valueOf(fileInfoModule.getLastModified())));
}else{
Pub.showMsgInfo(this, "找不到檔案!");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
File rootDir = new File(rootPath);
if(!rootDir.mkdirs()) Pub.showMsgInfo(this, "Note目錄建立失敗!");
}
}
}
點選列表跳轉閱讀介面
Intent intent = new Intent(this, NoteReadActivity.class);
Bundle bundle = new Bundle();
bundle.putString("filename", lstFile.get(i).getFileName());
bundle.putString("filepath", lstFile.get(i).getFielPath());
bundle.putString("lastmodified", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(lstFile.get(i).getLastModified()));
intent.putExtras(bundle);
startActivity(intent);
根據檔案路徑讀取檔案內容
private String getFileContent(String filePath){
if(filePath.equals("")) Pub.showMsgError(this,"檔案路徑為空!");
try {
File filename = new File(filePath);
FileInputStream fin = new FileInputStream(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(fin));
String fileContent = "";
String line = "";
while ((line = reader.readLine()) !=null){
fileContent = fileContent + line + "\n";
}
reader.close();
fin.close();
return fileContent;
}catch (Exception e){
Pub.showMsgError(this, e.getMessage());
}
return "";
}
儲存檔案
private void saveContent() {
//如果標題為空,則生成當前日期的標題
String filename = "";
if (mView.etNoteeditTitle.getText().toString().equals("")) {
filename = new SimpleDateFormat("yyyyMMdd").format(System.currentTimeMillis());
} else filename = mView.etNoteeditTitle.getText().toString();
File file = new File(filePath);
if (file.isDirectory()) {
file = new File(file.getPath(), filename + ".zgn");
if (file.exists()) {
filename = new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis());
file = new File(file.getParent(), filename + ".zgn");
}
} else {
String oldfilename = file.getName().substring(0, file.getName().lastIndexOf('.'));
if (!oldfilename.equals(filename)) {
String parentpath = file.getParent();
file.delete();
file = new File(parentpath, filename + ".zgn");
}
}
try {
FileOutputStream fout = new FileOutputStream(file);
fout.write(mView.etNoteeditContent.getText().toString().getBytes());
fout.close();
Intent intent = new Intent(this, NoteReadActivity.class);
Bundle bundle = new Bundle();
bundle.putString("filename", file.getName().substring(0, file.getName().lastIndexOf('.')));
bundle.putString("filepath", file.getAbsolutePath());
bundle.putString("lastmodified", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
intent.putExtras(bundle);
startActivityByAnim(intent);
NoteEditActivity.this.finish();
} catch (Exception e) {
Pub.showMsgInfo(this, e.getMessage());
}
}