diaconu liviu
diaconu liviu

Reputation: 1062

Android Marshmallow requestPermissions WRITE_EXTERNAL_STORAGE

After three days of searching I realized that the android 6 or change permissions. But I fail to ask oermisiunea to save a file. I probably groza examples but it did not, I did something wrong. 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

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. 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();

        }

Upvotes: 0

Views: 2738

Answers (2)

anthorlop
anthorlop

Reputation: 1881

To simplify your code I recommend you to use next code:

  1. Create a OnPermissionRequested interface:

    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(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

Upvotes: 0

diaconu liviu
diaconu liviu

Reputation: 1062

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();
        }
    }

Upvotes: 3

Related Questions