Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()公有绝对路径
下载:
private void downLoadNewVersion() { OkHttpUtils.get() .url(url) .headers(PackOkHttpUtils.getHeadsMap(this)) .tag(MainActivity.class) .addParams("version", CommonParamsUtil.getVersion(getApplicationContext())) .build() .execute(new FileCallBack(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), "ypw.apk") { @Override public void inProgress(float progress, long total, int id) { super.inProgress(progress, total, id); int progressInt = (int) (progress * 100); progressDialog.setProgress(progressInt); } @Override public void onError(Call call, Exception e, int id) { MyToast.showMessage("下载失败"); List<AppCompatActivity> activities = ((MyAppLication) getApplication()).getActivities(); for (AppCompatActivity activity : activities) { activity.finish(); } } @Override public void onResponse(File response, int id) { SPUtils.putBoolean("VersionUpdate", false); progressDialog.dismiss(); Uri uri = Uri.fromFile(response); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0系统 uri = FileProvider.getUriForFile (MainActivity.this, "com.youpinwallet.ypw.provider", response); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件 } intent.setDataAndType(uri, "application/vnd.android.package-archive"); startActivity(intent); } }); }
AndroidManifest.xml
<!-- 适配7.0系统数据 --> <provider android:name="android.support.v4.content.FileProvider" android:authorities="自己的包名.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>
<?xml version="1.0" encoding="utf-8"?> <resources> <paths> <external-path path="" name="camera_photos" /> </paths> </resources>