我按照此代码删除apk文件,然后再下载另一个,然后再安装
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = getResources().getString(R.string.apkname) + ".apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
File file = new File(destination);
if (file.canRead()) {
if (file.delete()) {
Toast.makeText(getApplicationContext(), "File deleted", Toast.LENGTH_LONG).show();
}
}
我找到了这个文件,所以我得到了file.delete(),然后我收到了toast的消息,但当我检查下载时,该文件仍然存在,我最终得到了多个文件相同的名字,我仍然可以毫无问题地使用它们,所以它就像file.delete()完全没有任何关系。
对于记录,我在清单中有WRITE_EXTERNAL_STORAGE权限,我也在运行时请求它,因为我使用sdkversion 25。
出了什么问题?
提前致谢
答案 0 :(得分:0)
尝试我测试过的这个解决方案......
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = getResources().getString(R.string.apkname) + ".apk";
destination += fileName;
try {
File file = new File(destination);
if (file.canRead()) {
boolean mDeleleFile = file.delete();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(destination))));
if (mDeleleFile) {
Toast.makeText(getApplicationContext(), "File deleted", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}