简体   繁体   English

棉花糖中的Android存储权限

[英]Android storage permission in Marshmallow

I have a problem in downloading file with my android app with android version>=6. 我在使用Android版本> = 6的android应用下载文件时遇到问题。

It works well in other versions but when i open it in android 6 and up, it crashes and closes. 它在其他版本中效果很好,但是当我在android 6及更高版本中打开它时,它崩溃并关闭。

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;
    }
}'

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: 我是刚开始编写android应用程序的新手,所以我可能会误会,但是要修复该错误,请将此代码行放入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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM