Question: I have this code, but when I click at the Button the Gallery will open and when I choose a image the Gallery will close again. And nothing will happen.

public class MainActivity extends AppCompatActivity {

```
private Button loadimagebutton;
private ImageView imageView;

private static int RESULT_LOAD_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadimagebutton = (Button) findViewById(R.id.button);

    loadimagebutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        imageView = (ImageView)findViewById(R.id.imageView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
    else{
        Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG).show();
    }
}

```

What is wrong?

Answer: 
You need to pass the parent injector like

```
export class MyIfDirective {
  constructor(private injector:Injector, private _viewContainer: ViewContainerRef,
    private _template: TemplateRef<Object>) 
  { }

  @Input() set myIf(rule: string) {
    let resolvedProviders = ReflectiveInjector.resolve([AdminOnly]);
    let childInjector = ReflectiveInjector.fromResolvedProviders(resolvedProviders, this.injector);

    let adminOnly : IRule = childInjector.get(AdminOnly);
    let show = adminOnly.shouldShowElement();
    show ? this.showItem() : this.hideItem();
  }
  private showItem() {
    this._viewContainer.createEmbeddedView(this._template);
  }

  private hideItem() {
    this._viewContainer.clear();
  }
}

```

See also [Inject service with ReflectiveInjector without specifying all classes in the dependency tree](https://stackoverflow.com/questions/40402169/inject-service-with-reflectiveinjector-without-specifying-all-classes-in-the-dep/40402201#40402201)
Just update for Angular version 10+:

* From your service:

```
  @Injectable({
    providedIn: 'any'
  })
  export class AdminOnly { ... }

```

* In your directive or a pure function, ...:

```
 import { Injector } from '@angular/core';

 ...
 const injector: Injector = Injector.create({
   providers: [{provide: AdminOnly, deps: []}]
 });
 const adminOnly: AdminOnly = injector.get(AdminOnly);

 let show = adminOnly.shouldShowElement();
 ...

```

[See more](https://angular.io/api/core/Injector)