[英]Saving and deleting file in Android Internal storage - deleting not working
我一直在使用此代碼刪除存儲在內部存儲中的文件。 但該文件沒有被刪除。
editTextFileName = findViewById(R.id.editTextFileName);
FileName = editTextFileName.getText().toString() + ".txt";
buttonDeleteFile = findViewById(R.id.buttonDeleteFile);
buttonDeleteFile.setOnClickListener(view -> {
LayoutInflater layoutInflater = LayoutInflater.from(DeleteFile.this);
View promptView = layoutInflater.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(DeleteFile.this);
alertDialogBuilder.setView(promptView);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", (dialog, id) -> {
Toast.makeText(this, "Deleting the file...", Toast.LENGTH_SHORT).show();
// get user input and set it to result
File dir = getFilesDir();
File file = context.getFileStreamPath(FileName);
if(file.exists()){
Toast.makeText(this, "temp" , Toast.LENGTH_SHORT).show();
try {
context.deleteFile(FileName);
Toast.makeText(this, "Successfully deleted", Toast.LENGTH_SHORT).show();
} catch (Exception e){
Toast.makeText(this, "Error " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
})
我無法弄清楚問題所在。
嘗試使用file.delete()
這絕對應該是工作。
文件目錄 = getFilesDir(); 文件文件 = context.getFileStreamPath(FileName);
您沒有使用dir
。
File file = new File(dir, FileName);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.