6

in my Android app i need to ask for permissions to write on external storage. Since Android 6, you have to ask for the permissions at run-time.

My problem is, I don't know how to resume my origin task after asking for the permissions because my function got passed an argument (soundId) which is "lost" after asking for the permissions. How to pass arguments when asking for permissions ?

My code looks like this:

MainActivity.java

static final int REQ_CODE_EXTERNAL_STORAGE_PERMISSION = 45;

public static void sharesound(int soundId, final Context context){
if (ActivityCompat.checkSelfPermission(context,    Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
  CopyRAWtoSDCard(soundId, soundpath,context);
} 

else 
{
  ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_CODE_EXTERNAL_STORAGE_PERMISSION);
}
}



@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == REQ_CODE_EXTERNAL_STORAGE_PERMISSION && grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED )
    {
      sharesound(soundId,getApplicationContext()); **I cannot access soundId from here !** 
    }
}
1
  • Put soundId in a field of this activity. Dec 18, 2016 at 19:32

2 Answers 2

5

EDIT:// This is outdated, better use ContentProvider or Fileprovider as no permissions are required for them!

I have found the solution by myself now:

first, we need a global variable:

 int lastsharedsound;

Then, the sharesound function saves the last shared sound into that variable.

public static void sharesound(int soundId)
{
    lastsharedsound=soundId;
    if (ActivityCompat.checkSelfPermission(context,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
    {
      CopyRAWtoSDCard(soundId, soundpath,context);
      ***do the intent sharing stuff here***
    } 

    else 
    {
      ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},     5);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == 5 && grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED )
    {
       sharesound(lastsharedsound); ***Here we call our function again*** 
    }
}
0
public static void sharesound(int soundId, final Context context){
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
  CopyRAWtoSDCard(soundId, soundpath,context);
} 

else 
{
  ActivityCompat.requestPermissions((Activity) context, new String[ {Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_CODE_EXTERNAL_STORAGE_PERMISSION);
  if(ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
      sharesound(soundId, context);
  }
}
}

Edit: I added a check for the permission before calling the method again

2
  • Thank you, that doesn't really work, if I click on deny the app hangs (seems like it is recursively trying to share sound) If i accept, the share dialog opens but fails cause the file is not copied to storage. Dec 18, 2016 at 22:39
  • Thank you very much for still trying to help me, I tried your edit but the second call of sharesound is not executed. I put a Toast in there and it did also not show up. Seems like the function is not waiting for the user to answer request Permission dialog. it just continues Dec 19, 2016 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.