0

I am using this code in async to create directory if not exists in android with permissions. My code sucessfully works in android 5.1 but when i deployed app in android 7.0 the directory not created automatically

   File sdcard = Environment.getExternalStorageDirectory() ;
        File folder = new File(sdcard.getAbsoluteFile(), "Quoteimages");
        if(!folder.exists()){
            folder.mkdir();

        }

Manifest file is

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

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

Link to detail of application apk App apk detail link

Now what should i do i want to create folder write images from url to it and then read them. This work for me in android 5.1 but not in Android version 7 .

Nouman
  • 17
  • 6
  • https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – CommonsWare Sep 30 '17 at 15:12
  • 1
    Also, there are no permissions named `READ_INTERNAL_STORAGE` or `WRITE_INTERNAL_STORAGE`. – CommonsWare Sep 30 '17 at 15:12
  • 2
    Possible duplicate of [How to check Grants Permissions at Run-Time?](https://stackoverflow.com/questions/30549561/how-to-check-grants-permissions-at-run-time) – Pavneet_Singh Sep 30 '17 at 15:15

1 Answers1

-1

You need to implement runtime permissions for android versions above lollipop

Maybe this piece of code will help you:

private static final int PERMS_REQUEST_CODE = 123;

//...........................................................................................................

 private boolean hasPermissions(){
    int res = 0;
    //string array of permissions,
    String[] permissions = new String[]
{android.Manifest.permission.WRITE_EXTERNAL_STORAGE};

    for (String perms : permissions){
        res = checkCallingOrSelfPermission(perms);
        if (!(res == PackageManager.PERMISSION_GRANTED)){
            return false;
        }
    }
    return true;
}

private void requestPerms(){
    String[] permissions = new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        requestPermissions(permissions,PERMS_REQUEST_CODE);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    boolean allowed = true;

    switch (requestCode){
        case PERMS_REQUEST_CODE:

            for (int res : grantResults){
                // if user granted all permissions.
                allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
            }

            break;
        default:
            // if user not granted permissions.
            allowed = false;
            break;
    }

    if (allowed){
        //user granted all permissions we can perform our task.
        ListItem listItem = new ListItem();
        Glide.with(getApplicationContext()).asBitmap().load(listItem.getImgurl()).into(new SimpleTarget<Bitmap>(){



            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                FileOutputStream fileOutputStream = null;
                File file = getDisc();
                if(!file.exists()&& !file.mkdirs()){

                    Toast.makeText(getApplicationContext(),"Can't create directory to save image", Toast.LENGTH_LONG).show();
                    return;
                }

                SimpleDateFormat simpleDataFormat = new SimpleDateFormat("yyyymmsshhmmss");
                String date = simpleDataFormat.format(new Date());
                String name = "img"+date+".jpg";
                String file_name = file.getAbsolutePath()+"/"+name;
                File new_file= new File(file_name);
                try {
                    fileOutputStream=new FileOutputStream(new_file);
                    resource.getHeight();
                    resource.getWidth();
                    resource.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
                    Toast.makeText(getApplicationContext(),"SAVED", Toast.LENGTH_LONG).show();
                    fileOutputStream.flush();
                    fileOutputStream.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                refreshGallery(new_file);

            }
            public void refreshGallery(File file){

                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                intent.setData(Uri.fromFile(file));
                sendBroadcast(intent);

            }


            private File getDisc(){



                File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                return new File(file, "FolderNamerYourChoice");
            }
        });

    }
    else {
        // we will give warning to user that they haven't granted permissions.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                Toast.makeText(this, "Storage Permissions denied.", Toast.LENGTH_SHORT).show();
            }
        }
    }
Sebin Paul
  • 169
  • 1
  • 15