WebView. SetDownloadListener (new DownloadListener () {
Public void onDownloadStart (url String, the String userAgent,
String contentDisposition, String mimetype,
Long contentLength) {
DownloadManager. Request Request=new DownloadManager. Request (Uri. Parse (url));
Request. AllowScanningByMediaScanner ();
Request. SetTitle (" test ");
Request. SetDescription (" download.. ");
The String fileName=URLUtil. GuessFileName (url, contentDisposition, mimetype);
Request. SetNotificationVisibility (
DownloadManager. Request. VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Request. SetDestinationInExternalPublicDir (
Environment. DIRECTORY_DOWNLOADS,
FileName);
DownloadManager dm=(DownloadManager) getSystemService (
DOWNLOAD_SERVICE);
Toast. MakeText (getApplicationContext (), "to start the download," Toast. LENGTH_LONG), show ();
dm. The enqueue (request);
}
});
The red place is where the below 171
An error is as follows:
Process: com. Example. Yiznan, PID: 6840
Java. Lang. SecurityException: No permission to write to/storage/emulated/0/Download/1 _20200525172223237760. PNG: Neither the user nor 10079 current process from android. Permission. WRITE_EXTERNAL_STORAGE.
. An android OS. Parcel. ReadException (2004) Parcel. Java:
An android. Database. DatabaseUtils. ReadExceptionFromParcel (DatabaseUtils. Java: 183)
An android. Database. DatabaseUtils. ReadExceptionFromParcel (DatabaseUtils. Java: 135)
An android. Content. ContentProviderProxy. Insert (ContentProviderNative. Java: 476)
An android. Content. ContentResolver. Insert (ContentResolver. Java: 1539)
. An android app. DownloadManager. The enqueue (DownloadManager. Java: 1022)
At com. Example. The test. The MainActivity $4. OnDownloadStart (MainActivity. Java: 171)
The at xl. A (PG: 519)
At afG. HandleMessage (PG: 25)
An android. OS. Handler. DispatchMessage (Handler. Java: 106)
. An android OS. Stars. Loop (164). Which Java:
. An android app. ActivityThread. Main (6494) ActivityThread. Java:
The at Java. Lang. Reflect. Method. Invoke (Native Method)
At com. Android. Internal. OS. RuntimeInit $MethodAndArgsCaller. Run (438) RuntimeInit. Java:
At com. Android. Internal. OS. ZygoteInit. Main (ZygoteInit. Java: 807)
The permissions are added android. Permission. WRITE_EXTERNAL_STORAGE
Tell for bosses
CodePudding user response:
You there is no authority, the high version of the need to apply dynamic, you can download the code when older, no related code,Dynamic application permissions, give you stick example:
Private final static int mRequestCode=10000;//permission request code, self definition, mainly when system callback will be back, used to distinguish between
Public static void checkPermisstion (Activity to Activity, String [] permissions) {
The d (tag, "start check permissions:" + Build. VERSION. SDK_INT);
ListMPermissionList=new ArrayList<> (a);
If (Build) VERSION) SDK_INT & gt;=Build. VERSION_CODES. M) {
The d (tag, "requires dynamic authorization");
MPermissionList. The clear ();
//one by one to judge whether the permissions you want already by
for (int i=0; I & lt; Permissions. Length; I++) {
If (ContextCompat. CheckSelfPermission (activity, permissions [I])!=PackageManager. PERMISSION_GRANTED) {
MPermissionList. Add (permissions [I]);//add has not been granted permission
The d (tag, "unauthorized" + permissions [I]);
} else {
The d (tag, "existing authority:" + permissions [I]);
}
}
//application permissions
If (mPermissionList. The size () & gt; 0) {//permissions didn't pass, need to apply for the
ActivityCompat. RequestPermissions (activity, permissions, mRequestCode);
}
}
}
//then where need to apply for, such as the activity of onStart
@ Override
Protected void onStart () {
Super. OnStart ();
The d (tag, "main interface onStart");
String [] permissions={Manifest. Permission. WRITE_EXTERNAL_STORAGE};//this is an array, can apply for multiple access at the same time
CheckPermisstion (this, permissions);
}
CodePudding user response:
Also missed a method, to apply for permission, the system will pop up to apply for permission to interface, the user action and system callback after this method, from this method can see whether the user has chosen the authorization
@ Override
Public void onRequestPermissionsResult (int requestCode, @ NonNull String [] permissions, @ NonNull int [] grantResults) {
If (requestCode==mRequestCode) {
Boolean isNotAllPermission=false;
for (int i=0; I & lt; Permissions. Length & amp; & I & lt; GrantResults. Length; I++) {
//if records have permission option users did not give permission to
If (grantResults [I]==PackageManager. PERMISSION_DENIED) {
IsNotAllPermission=true;
break;
}
}
If (isNotAllPermission) {
The d (tag, "did you get all the authorization, it will affect applications use");
//todo
}
}
}
CodePudding user response:
Also, looks like you don't need to use this in your code, can directly use the system default directory, for example when download is only temporary storage, can call this method to obtain directory where download directory:
/* *
* for download directory, external storage priority
*
* @ param context context
* @ return File directory File object
*/
Public static File getDownloadFolder Context (Context) {
If (Environment. GetExternalStorageState (.) the equals (Environment. MEDIA_MOUNTED)
& & ! Environment. IsExternalStorageRemovable ()) {
Return the context. GetExternalFilesDir (Environment. DIRECTORY_DOWNLOADS);
} else {
nullnullnullnull