Unable To Download Files Using Download Manager In Emulator
Android Studio 2.1.2, API 23 Error: java.lang.SecurityException: No permission to write to/storage/emulated/0/Download/aabd.pdf: Neither user 10059 nor current process ha
Solution 1:
You have to check permission like this if you have targetSdk 23
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
checkPermission();
}
else {
Filefile=newFile(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), nameOfFile);
request.setDestinationInExternalPublicDir
(Environment.DIRECTORY_DOWNLOADS, nameOfFile);
request.setVisibleInDownloadsUi(true);
myDownloadReference = downloadManager.enqueue(request);
}
privatevoidcheckPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {//Can add more as per requirement
ActivityCompat.requestPermissions(this,
newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
123);
} else {
}
}
@OverridepublicvoidonRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case123: {
// If request is cancelled, the result arrays are empty.if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Filefile=newFile(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), nameOfFile);
request.setDestinationInExternalPublicDir
(Environment.DIRECTORY_DOWNLOADS, nameOfFile);
request.setVisibleInDownloadsUi(true);
myDownloadReference = downloadManager.enqueue(request);
} else {
checkPermission();
}
return;
}
}
}
Solution 2:
Is sd card emulation is enabled in emulator? you may want to use Genymotion emulator instead of built-in
Post a Comment for "Unable To Download Files Using Download Manager In Emulator"