Adapter中如何获取WRITE_EXTERNAL_STORAGE权限?

How to get WRITE_EXTERNAL_STORAGE Permission in Adapter?

我想从远程 url 下载图像,我需要获得用户许可才能保存图像,但我无法在 GridAdapter.java 文件中执行此操作,这在适配器中不起作用:

requestPermissions((Activity) context,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},DOWNLOAD_REQUEST_CODE);

我尝试了很多网上的方法,但似乎没有任何效果,

喜欢ActivityCompat.requestPermissions(context,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},DOWNLOAD_REQUEST_CODE);

 requestPermissions((Activity) context.getApplicationContext(),new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},DOWNLOAD_REQUEST_CODE);

在GridActivity中class创建一个接口并在GridAdapter中实现它class

interface IPermissionCallback{
    void askForStoragePermission();
    Boolean checkSelfStoragePermission();
}

public class GridActivity extends AppCompatActivity implements 
    IPermissionCallback {

    private static final int DOWNLOAD_REQUEST_CODE = 1001;
    private GridAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable 
        PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    
        //other codes...
        //initialize GridAdapter here and then call setPermissionCallback() 
        //method as shown below
        adapter.setPermissionCallback(this);
    }


    @Override
    public void askForStoragePermission() {
        requestPermissions(new String[] 
        {android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
        DOWNLOAD_REQUEST_CODE);
    }

    @Override
    public Boolean checkSelfStoragePermission() {
    
        if(checkCallingOrSelfPermission(
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == 
            PackageManager.PERMISSION_GRANTED){
                return true;
        }
        return false;
    }


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

          if (requestCode == DOWNLOAD_REQUEST_CODE 
          && grantResults[0] == PackageManager.PERMISSION_GRANTED){
               //permission granted update GridAdapter to do next task
               adapter.onPermissionResult(true);
          }else{
               //permission denied update GridAdapter to do next task
               adapter.onPermissionResult(false);
          }
     }
 }

并且在 Grid Adapter 中你必须编写这些方法

public class GridAdapter/**extends needed classes etc.*/{
     private IPermissionCallback callback;


     public void yourMethodWhereYouDownloadingImg(){
         if (callback != null && callback.checkSelfStoragePermission()){
             //already permission is granted
         }else{
            callback.askForStoragePermission();
         }
     }

     public void setPermissionCallback(IPermissionCallback c){
         this.callback = c;
     }

     public void onPermissionResult(Boolean permissionGranted){
         /**
           * if @permissionGranted is true download image else inform user 
             that permission is denied
           */
     }
}