删除下载的文件android

时间:2019-05-04 12:57:50

标签: android android-download-manager android-external-storage

因此,我已使用DownloadManager使用以下代码从Internet下载文件:

public void DownloadFile()
    {
        Log.d("DownloadFileEntered", "true");

        try {
            String url = "https://firebasestorage.googleapis.com/v0/b/roti-bank-testing.appspot.com/o/SocialGallery%2Fsome.jpg?alt=media&token=d2ddd40f-0c04-441c-82b1-585fd8743b19";
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "somegif.gif");
            DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            long d = manager.enqueue(request);


        }catch (Exception e)
        {
            Log.d("DownloadFileEntered", "Error."+e.toString());
        }
    }

现在说,我要删除此文件。 为此,我使用了以下代码:

    File file = new File("file://" + Environment.DIRECTORY_DOWNLOADS + "/somegif.gif");
    boolean d = file.delete();
    Log.d("DownloadFileEntered", "D : "+d);

但是很显然,最后一个Log.d给出的输出为false,并且该文件也不会被删除。那么,从外部存储中删除文件的正确方法是什么?

还可以在下载完成后运行功能吗?如果是,那怎么办?

1 个答案:

答案 0 :(得分:0)

希望这对您有帮助:

add_filter('acf/load_field/name=chef', 'my_acf_load_chef_field');

function my_acf_load_chef_field( $field )
{    
    $user_fields = array( 'user_login');
    $argu = new WP_User_Query( array( 'role' => 'chef' , 'fields' => $user_fields ));
    $choices = $argu->get_results();
    //$choices = get_field($field['choices'], $post->ID , false);
    $field['choices'] = array();  
     if( is_array($choices) ) {
        $len = count($choices);
        for($i = 0; $i < $len; $i++) {
            array_push($field['choices'], ($choices[$i]->user_login));
    }
   }
    // var_dump($field['choices']);
    // exit;
    return $field;

不要忘记在manifest.xml中设置此权限:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/somegif.gif");
    boolean d = file.delete();
    Log.d("DownloadFileEntered", "D : "+d);