I have a problem in downloading file with my android app with android version>=6.
It works well in other versions but when i open it in android 6 and up, it crashes and closes.
I get the following error:
java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/MP3 BOX
at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:538)
at com.org.software.myproject.common.adapters.DataAdapter$1.onClick(DataAdapter.java:87)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I did the following according to other solution from here but it didnt work out:
'
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
//Permission granted
return true;
} else {
//Permission invoked
ActivityCompat.requestPermissions(this, new String[]{WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
//Permission granted
return true;
}
}'