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 !**
}
}
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***
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With