diaconu liviu diaconu liviu - 4 months ago 51
Android Question

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

}

Answer

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