Why the DowanloadManager was not working in Android Emulator Device?

43 views Asked by At

I was testing DownloadManager in Android Emulator device,but it did not work, this is the code:

String apkPath = getApkPath(context);
File fileFolder = new File(path);
if (!fileFolder.exists()) {
    file.mkdirs();
}
apkPath += "test.apk";
File file = new File(apkPath);

DownloadManager downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);

            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadUrl))
                    .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
                    .setVisibleInDownloadsUi(true)
                    .setMimeType("application/vnd.android.package-archive")
                    .setDestinationUri(Uri.fromFile(file));

the getApkPath method:

public String getApkPath(Context context){
    return  getDiskCacheDir(context) + "/CheckManager/apk" + File.separator;
}

the getDiskCacheDir method is:

public String getDiskCacheDir(Context context) {
        String cachePath = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            File file=context.getExternalFilesDir(null);
            if(file==null){
                return cachePath;
            }
            if(!file.exists()){
                file.mkdir();
            }
            cachePath = file.getPath();
        } else {
            cachePath = context.getFilesDir().getPath();
        }
        return cachePath;
    }

anyone can help me?

0

There are 0 answers