file.delete返回true,但是文件未删除

问题描述

我将文件从URL下载到外部存储,然后将数据复制到内部存储,然后需要从外部存储中删除文件,我的复制方法如下:

public static void copyDataBase(String databaseName,String ExternalDatabaseName) throws IOException {

        String DB_NAME = databaseName;
        String NEW_DB_NAME = DB_NAME.replace("-","_");
        String DB_PATH = MyApplication.getAppContext().getDatabasePath(NEW_DB_NAME).getPath();
        Log.d("pathx","path: " + DB_PATH);

        // Open your local db as the input stream
        String fileName = ExternalDatabaseName;
        String newFileName = fileName.replace("_","-");
        //String path = Environment.DIRECTORY_DOWNLOADS+"/"+fileName;
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),fileName);
        FileInputStream inputStream = new FileInputStream(file);

        // Path to the just created empty db
        String outFileName = DB_PATH;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the input file to the output file
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            myOutput.write(buffer,length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        inputStream.close();

        if(ContextCompat.checkSelfPermission(MyApplication.getAppContext(),Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            boolean deleted = file.delete();
        }
    }

如您所见,在复制后,我尝试使用相同的路径删除相同的文件,但是即使已删除的布尔值返回true,也不会从设备中删除文件,而是下载文件

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)