protected void doImportSelectionAction(Program program, ProgramSelection selection) {
		if (selection == null || selection.getNumAddressRanges() != 1) {
			return;
		}

		AddressRange range = selection.getFirstRange();// should only be 1
		if (range.getLength() >= Integer.MAX_VALUE) {
			Msg.showInfo(getClass(), tool.getActiveWindow(), "Selection Too Large",
				"The selection is too large to extract.");
			return;
		}

		try {
			Memory memory = program.getMemory();

			// create a tmp ByteProvider that contains the bytes from the selected region
			FileCacheEntry tmpFile;
			try (FileCacheEntryBuilder tmpFileBuilder =
				fsService().createTempFile(range.getLength())) {
				byte[] bytes = new byte[(int) range.getLength()];
				memory.getBytes(range.getMinAddress(), bytes);
				tmpFileBuilder.write(bytes);
				tmpFile = tmpFileBuilder.finish();
			}

			MemoryBlock block = memory.getBlock(range.getMinAddress());
			String rangeName =
				block.getName() + "[" + range.getMinAddress() + "," + range.getMaxAddress() + "]";
			ByteProvider bp =
				fsService().getNamedTempFile(tmpFile, program.getName() + " " + rangeName);
			LoaderMap loaderMap = LoaderService.getAllSupportedLoadSpecs(bp);

			ImporterDialog importerDialog = new ImporterDialog(tool,
				tool.getService(ProgramManager.class), loaderMap, bp, null);
			tool.showDialog(importerDialog);
		}
		catch (IOException e) {
			Msg.showError(this, null, "I/O Error Occurred", e.getMessage(), e);
		}
		catch (MemoryAccessException e) {
			Msg.showError(this, null, "Memory Access Error Occurred", e.getMessage(), e);
		}
	}
A {@link Plugin} that supplies menu items and tasks to import files into Ghidra.
Ghidra/Features/Base/src/main/java/ghidra/plugin/importer/ImporterPlugin.java