0

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;
    }
}'
  • https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare Nov 30 '16 at 20:02
  • i tried that but it didnt work, i dont know why? i can post the whole code here but i think this part has some problem –  Nov 30 '16 at 20:07

1 Answers1

0

Im fairly new to writing android apps, so I could be mistaken, but to fix that error put this line of code in the onCreate:

ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                1);

You also need to add the uses permission lines in the manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Hope this helps!

Alex Collette
  • 1,664
  • 13
  • 26
  • Thanks alot... But now i face a new problem.. i get the permission request notification everytime i search or want to download a file. –  Dec 01 '16 at 06:50
  • Hm.. I would try putting the code that i replied with inside of the if statement you have. It will work so long as it gets called before you try to download files. you may need to call that if statement inside of the onCreate. – Alex Collette Dec 01 '16 at 16:44