简体   繁体   English

Android棉花糖requestPermissions WRITE_EXTERNAL_STORAGE

[英]Android Marshmallow requestPermissions WRITE_EXTERNAL_STORAGE

After three days of searching I realized that the android 6 or change permissions. 经过三天的搜索,我意识到android 6或更改权限。 But I fail to ask oermisiunea to save a file. 但是我没有要求oermisiunea保存文件。 I probably groza examples but it did not, I did something wrong. 我可能是groza示例,但是没有,我做错了事。 Please help. 请帮忙。

    String url = "http://grupovrt.ddns.net:81/v4.2.apk";
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setDescription("Virtual Romania Tv UPDATE");
        request.setTitle("UPDATE Virtual Romania Tv");
// in order for this if to run, you must use the android 3.2 to compile your app
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        }
        if (!checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                R.string.title_activity_categori))
        {
            Toast.makeText(getBaseContext(), "Not allowed to save files", Toast.LENGTH_SHORT).show();
        } else {

            // try to save the file

            request.setDestinationInExternalPublicDir("/update", "v4.2.apk");

        }


// get download service and enqueue file
        DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        manager.enqueue(request);

Error: Error:(108, 49) error: cannot find symbol variable WRITE_EXTERNAL_STORAGE 错误:错误:(108,49)错误:找不到符号变量WRITE_EXTERNAL_STORAGE

Edit. 编辑。

I did seek if given permission to write. 我确实在寻求书面许可。 but I do not know how to ask permission if it is not. 但我不知道如何获得许可。 Please help me, I tried everything I found on google but could not. 请帮助我,我尝试了我在Google上发现的所有内容,但未能成功。 I'm a beginner. 我是初学者。

 if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {

            Toast.makeText(getBaseContext(), "Descarc noi actualizari!", Toast.LENGTH_SHORT).show();



            String url = "http://grupovrt.ddns.net:81/v4.2.apk";
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.setDescription("Virtual Romania Tv UPDATE");
            request.setTitle("UPDATE Virtual Romania Tv");

            // in order for this if to run, you must use the android 3.2 to compile your app

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                // get download service and enqueue file
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);


                Toast.makeText(getBaseContext(), "Descarc noi actualizari!", Toast.LENGTH_SHORT).show();

            }


            request.setDestinationInExternalPublicDir("/update", "v4.2.apk");
        }else {
            Toast.makeText(getBaseContext(), "Nu ai permisiunea sa descarci!", Toast.LENGTH_SHORT).show();

        }

This is working for me: 这为我工作:

    int permissionCheck = ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(context, "Trebuie sa oferi acces la spatiul de stocare!", Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(context, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 110);
            ActivityCompat.requestPermissions(context, new String[]{android.Manifest.permission.RECORD_AUDIO}, 110);
        } else {
            Toast.makeText(context, "Descarc noi actualizari!", Toast.LENGTH_SHORT).show();
        }
    }

To simplify your code I recommend you to use next code: 为了简化您的代码,我建议您使用下一个代码:

  1. Create a OnPermissionRequested interface: 创建一个OnPermissionRequested接口:

     public interface OnPermissionRequested { void onGranted(); } 
  2. In activities/fragments add next methods to check permission. 在活动/片段中,添加下一个方法来检查权限。 You also can create a main activity and fragment and extend to avoid duplicated code: 您还可以创建一个主要活动和片段,并进行扩展以避免重复的代码:

     OnPermissionRequested mPermissionRequest; protected void requestPermission(String permission, OnPermissionRequested listener) { mPermissionRequest = listener; // Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) { Toast.makeText(this, "Permission denied, try again later please.", Toast.LENGTH_SHORT).show(); } else { // request the permission. ActivityCompat.requestPermissions(this, new String[]{permission}, 2456); } } else { mPermissionRequest.onGranted(); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (mPermissionRequest != null) mPermissionRequest.onGranted(); } } 
  3. Call requestPermission method where you need check the permission and set the second argument with the action you want to do if the permission is granted. 在需要检查许可权的地方调用requestPermission方法,并在授予许可权时设置要执行的操作的第二个参数。

     requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, new OnPermissionRequested() { @Override public void onGranted() { // what you want to do createPdf(); } }); 

I hope you find it useful. 希望对你有帮助。 Good luck 祝好运

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

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